如何运行在自己的线程中扭曲并正确停止?
How to run twisted in its own thread and stop properly?
我在我的应用程序中使用了 twisted,运行 它在它自己的线程中。像这样使用 twisted 效果很好,但它永远不会正确停止。
示例:
# Python 2.7.6
# Twisted==15.4.0
import time
import threading
from twisted.internet import reactor
print("starting reactor")
reactor_thread = threading.Thread(target=reactor.run,
kwargs={"installSignalHandlers": False})
reactor_thread.start()
print("reactor started")
time.sleep(1)
print("stopping reactor")
reactor.stop()
print("joining thread")
reactor_thread.join()
print("reactor stopped") # <- never reached
输出:
starting reactor
reactor started
stopping reactor
joining thread
您应该使用 Crochet。它 运行 是线程中的反应器。
您还应该认真考虑重构您的应用程序以尊重 Twisted 的习惯用法。这样做将允许您的应用程序与其他 Twisted 应用程序组合在一起,并减轻您的维护负担。 (然后您就不必 运行 线程中的反应器了。)
我在我的应用程序中使用了 twisted,运行 它在它自己的线程中。像这样使用 twisted 效果很好,但它永远不会正确停止。
示例:
# Python 2.7.6
# Twisted==15.4.0
import time
import threading
from twisted.internet import reactor
print("starting reactor")
reactor_thread = threading.Thread(target=reactor.run,
kwargs={"installSignalHandlers": False})
reactor_thread.start()
print("reactor started")
time.sleep(1)
print("stopping reactor")
reactor.stop()
print("joining thread")
reactor_thread.join()
print("reactor stopped") # <- never reached
输出:
starting reactor
reactor started
stopping reactor
joining thread
您应该使用 Crochet。它 运行 是线程中的反应器。
您还应该认真考虑重构您的应用程序以尊重 Twisted 的习惯用法。这样做将允许您的应用程序与其他 Twisted 应用程序组合在一起,并减轻您的维护负担。 (然后您就不必 运行 线程中的反应器了。)