在 Python 2 中有一个函数thread.interrupt_main(),KeyboardInterrupt当从子线程调用时,它会在主线程中引发异常。这也可以_thread.interrupt_main()在 Python 3 中使用,但它是一个低级的“支持模块”,主要用于其他标准模块。在 Python 3 中这样做的现代方法是什么,大概是通过threading模块,如果有的话?
2 回答

牛魔王的故事
TA贡献1830条经验 获得超3个赞
手动引发异常有点低级,所以如果您认为必须这样做,请使用,_thread.interrupt_main()
因为这与您要求的等效(threading
模块本身不提供此功能)。
不过,可能有一种更优雅的方式来实现您的最终目标。也许设置和检查一个标志就足够了threading.Event
,或者使用已经建议的类似@RFmyD,或者使用通过queue.Queue
. 这取决于您的具体设置。
添加回答
举报
0/150
提交
取消