在某种条件下停止扭曲的反应堆

时间:2011-06-29 20:35:27

标签: twisted reactor

当达到某种条件时,是否有办法停止扭曲的反应堆。例如,如果变量设置为某个值,那么反应堆应该停止吗?

2 个答案:

答案 0 :(得分:24)

理想情况下,您不会将变量设置为值并停止反应堆,您可以调用reactor.stop()。有时你不在主线程中,这是不允许的,所以你可能需要调用reactor.callFromThread。以下是三个有效的例子:

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

答案 1 :(得分:6)

确定:

if a_variable == 0:
    reactor.stop()
相关问题