pythonpython-2.7twistedtwisted.internet

Twisted reactor after specific period of time


Is there a way to stop reactor.run() after specific time, f.e 10 mins, via reactor.stop() and reactor.callFromThread(reactor.stop)

Sample:

import sys
from twisted.python import log
from twisted.internet import reactor, task
from twisted.internet.defer import Deferred, DeferredList
from autobahn.twisted.websocket import connectWS
from protocol import WampClientFactory, WampClientProtocol

class SMSManagementProtocol(WampClientProtocol):
    def showEvent(self, topicUri, result):
        print "EVENT:", topicUri,result
    def onSessionOpen(self):
        self.prefix("event", "http://test.tv/subscribe_public")
        self.subscribe('psevent:on_sms_delivered', self.showEvent)
        self.publish("event:send_sms", {'sms_text':'test','phone_number':29999999,'user_name':'test'})

if __name__ == '__main__':
    sms_factory = WampClientFactory("ws://xx.xx.xxx.x:xxxx", debugWamp = True)
    sms_factory.protocol = SMSManagementProtocol
    connectWS(sms_factory)
    reactor.run()

Is it possible to stop the reactor like this?

def check_stop_flag():
if reactor.run(600):
    reactor.stop()
lc = task.LoopingCall(check_stop_flag)
lc.start(600)

Solution

  • You can schedule any function call after a time delay with reactor.callLater:

    reactor.callLater(60 * 10, reactor.stop)