gpt4 book ai didi

python - 如何在自己的线程中扭曲运行并正确停止?

转载 作者:太空宇宙 更新时间:2023-11-03 17:18:44 25 4
gpt4 key购买 nike

我在我的应用程序中使用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 。它为您在线程中运行 react 器。

您还应该认真考虑重构您的应用程序以尊重 Twisted 的习惯用法。这样做将允许您的应用程序与其他 Twisted 应用程序组合在一起,并减轻您的维护负担。 (然后您就不必在线程中运行 react 器。)

关于python - 如何在自己的线程中扭曲运行并正确停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33397783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com