作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 Python GUI,用于测试我工作的各个方面。目前我有一个“停止”按钮,可以在每次测试结束时终止进程(可以同时运行多个测试)。但是,有些测试需要很长时间才能运行,如果我需要停止测试,我希望它立即停止。我的想法是用
import pdb; pdb.set_trace()
exit
但我不确定如何将其注入(inject)到下一行运行的代码中。这可能吗?
最佳答案
如果它是一个线程,你可以使用较低级别的thread
(或Python 3中的_thread
)模块通过调用异常杀死线程thread.exit()
.
来自documentation :
更简洁的方法(取决于您的处理设置方式)是使用实例变量向线程发出停止处理并退出的信号,然后调用 join()
方法从主线程等待直到线程退出。
例子:
class MyThread(threading.Thread):
def __init__(self):
super(MyThread, self).__init__()
self._stop_req = False
def run(self):
while not self._stop_req:
pass
# processing
# clean up before exiting
def stop(self):
# triggers the threading event
self._stop_req = True;
def main():
# set up the processing thread
processing_thread = MyThread()
processing_thread.start()
# do other things
# stop the thread and wait for it to exit
processing_thread.stop()
processing_thread.join()
if __name__ == "__main__":
main()
关于python - 如何从 Tkinter 窗口立即停止 Python 进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11938109/
我是一名优秀的程序员,十分优秀!