Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Twisted 在一定条件下停止扭曲反应器_Twisted_Reactor - Fatal编程技术网

Twisted 在一定条件下停止扭曲反应器

Twisted 在一定条件下停止扭曲反应器,twisted,reactor,Twisted,Reactor,当达到特定条件时,是否有办法停止扭曲反应器。例如,如果变量设置为某个值,则反应器应停止?确定: if a_variable == 0: reactor.stop() 理想情况下,您不会将变量设置为某个值并停止reactor,而是调用reactor.stop()。有时您不在主线程中,这是不允许的,因此您可能需要调用reactor.callFromThread。以下是三个工作示例: # in the main thread: reactor.stop() # in a non-main

当达到特定条件时,是否有办法停止扭曲反应器。例如,如果变量设置为某个值,则反应器应停止?

确定:

if a_variable == 0:
    reactor.stop()

理想情况下,您不会将变量设置为某个值并停止reactor,而是调用
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)

循环调用示例就是我要寻找的。不过,如果可以在reactor中添加一个事件,该事件会说,当设置了某个_标志时,调用某个_方法,该方法将调用reactor.stop.Thance youe detailed responseThank you发布此消息。有人想知道为什么reactor.stop()不默认从主线程调用。。。这似乎是不必要的复杂化,但我确信这是一个潜在的原因。TwistedAPI不是线程安全的。这是一种比让一些看似随机的子集可以从任何线程安全调用更简单的总括方法(这会让您永久性地参考文档,以确定要使用的任何特定API是否安全)。使所有API都是线程安全的是不切实际的(运行时开销是禁止的,在某些情况下实现变得非常复杂,API本身变得更加复杂-例如,如果工厂在非反应器线程中与connectTCP一起使用,那么调用工厂的方法是什么线程?)。