杀死python3.X中的所有进程和线程 [英] Killing all processes and threads in python3.X
问题描述
我正在编写一个UI包装器,以使用esptool.py读取一些信息
I'm writing a UI wrapper for reading some info using esptool.py
我有两个活动线程:UI和处理-SerialReader. UI类具有对SerialReader的引用,并且在获取退出命令时应停止SerialReader.
I have two active threads: UI and procesing - SerialReader. UI class has reference to the SerialReader and should stop SerialReader when it gets the exit command.
问题是我调用esptool命令,该命令陷入尝试通过串行连接读取数据的问题.
The problem is that I call esptool command which gets stuck in trying to read data over serial connection.
class SerialReaderProcess(threading.Thread):
def __init__(self, window):
super().__init__()
self.window = window
self.logger = window.logger
self.window.set_thread(self)
self._stop_event = threading.Event()
def run(self):
...
#read chip id
esptool.main(['chip_id'])
...
def stop(self):
self._stop_event.set()
def stopped(self):
return self._stop_event.is_set()
我想要的是杀死该程序的所有活动进程.当我调用关闭UI并调用serialReaderProcess.stop()
时,它不会停止该过程.我可以在控制台上看到esptool的输出.
What I want is to kill all active process of this program. When I call close the UI and call serialReaderProcess.stop()
it doesn't stop the process. I can see the output of esptool on the console.
我不在乎是否中断任何事情,没有数据会被破坏.
I don't care if I interrupt anything, no data can be corrupted.
我尝试sys.exit(0)
无济于事.
我已经研究了问题,但是找不到解决方案.
I've tried sys.exit(0)
to no avail.
I've researched the problem but couldn't find a solution.
操作系统是Ubuntu,我不在乎跨平台功能,但是它们会很好
The OS is Ubuntu and I don't care about cross-platform features, but they would be nice
推荐答案
如注释中所述,将线程设置为Daemon可以解决问题:
As stated in comments, setting the thread as Daemon solved the problem:
super().__init__(daemon=True)
程序退出时,守护进程线程将自动终止.
Daemon threads are automatically killed when the program quits.
有关守护程序的更多信息: 守护程序线程说明
More about daemons: Daemon Threads Explanation
这篇关于杀死python3.X中的所有进程和线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!