我在我的应用程序中使用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
答案 0 :(得分:1)
您应该使用Crochet。它为您运行线程中的反应器。
您还应该认真考虑重构您的应用程序以尊重Twisted的习语。这样做可以使您的应用程序与其他Twisted应用程序一起组合,从而减轻您的维护负担。 (然后你不必在一个线程中运行一个反应堆。)