特定时间后扭转反应器

时间:2018-04-03 13:22:46

标签: python python-2.7 twisted twisted.internet

有没有办法在特定时间后停止reactor.run(),f.e 10分钟,通过reactor.stop()reactor.callFromThread(reactor.stop)

样品:

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()

是否有可能像这样停止反应堆?

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

1 个答案:

答案 0 :(得分:0)

您可以使用reactor.callLater

延迟一段时间后安排任何函数调用
reactor.callLater(60 * 10, reactor.stop)