Stop twisted reactor on a condition

喜欢而已 提交于 2019-11-29 02:18:02

问题


Is there a way to stop the twisted reactor when a certain condition is reached. For example, if a variable is set to certain value, then the reactor should stop?


回答1:


Ideally, you wouldn't set the variable to a value and stop the reactor, you'd call reactor.stop(). Sometimes you're not in the main thread, and this isn't allowed, so you might need to call reactor.callFromThread. Here are three working examples:

# in the main thread:
reactor.stop()

# in a non-main thread:
reactor.callFromThread(reactor.stop)

# A looping call that will stop the reactor on a variable being set, 
# checking every 60 seconds.
from twisted.internet import task
def check_stop_flag():
    if some_flag:
        reactor.stop()
lc = task.LoopingCall(check_stop_flag)
lc.start(60)



回答2:


sure:

if a_variable == 0:
    reactor.stop()


来源:https://stackoverflow.com/questions/6526923/stop-twisted-reactor-on-a-condition

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!