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

gme*_*mon 18 twisted reactor

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

Jer*_*rub 26

理想情况下,您不会将变量设置为值并停止反应堆,您可以调用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)
Run Code Online (Sandbox Code Playgroud)


Ger*_*rat 6

当然:

if a_variable == 0:
    reactor.stop()
Run Code Online (Sandbox Code Playgroud)