处理Python线程中的信号 [英] Handling Signals in Python Threads
问题描述
我有一个用Python编写的线程应用程序,每当通过 Ctrl + C 接收到中断时,或者有时带有kill时,该应用程序将挂起.堆栈跟踪是从一个线程显示的,但是应用程序仍然在前台,我通常必须先使用 Ctrl + Z 使其成为后台尝试杀死它.
I have a threaded application written in Python, and whenever an interrupt is received via Ctrl+C or sometimes with kill, the application will hang. A stack trace is presented from one thread, but the application remains in the foreground, and I usually have to background it with Ctrl+Z then attempt to kill it.
在线程应用程序内部处理信号和键盘中断的正确方法是什么?
What is the proper way of handling signals and keyboard interrupts inside of a threaded application?
推荐答案
如果在启动每个线程之前设置了newthread.daemon = True
,则在主线程退出时这些线程将自动被杀死.这不完全是您要问的问题,但是从您的描述来看,这听起来可能是值得了解的.
If you set newthread.daemon = True
before starting each thread, the threads will automatically be killed when the main thread exits. That's not precisely what you were asking, but from what you've described, it sounds like it could be worth knowing.
这篇关于处理Python线程中的信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!