处理Python线程中的信号 [英] Handling Signals in Python Threads

查看:228
本文介绍了处理Python线程中的信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆