我正在编写一个UI包装器,以使用esptool.py读取一些信息
我有两个活动线程:UI和处理-SerialReader。 UI类具有对SerialReader的引用,并且在获取退出命令时应停止SerialReader。
问题是我调用esptool命令,该命令陷入尝试通过串行连接读取数据的问题。
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的输出。
我不在乎是否中断任何事情,没有数据会被破坏。
我尝试sys.exit(0)
无济于事。
我已经研究了问题,但是找不到解决方案。
操作系统是Ubuntu,我不在乎跨平台功能,但是它们会很好
答案 0 :(得分:0)
第一个导入操作系统库:
Import os
然后,您可以在exit_event方法中编写以下代码:
def closeEvent(self, event):
output,errors = p1.communicate()
bashCommand = "killall python3"
sudoPassword = 'your password'
p = os.system('echo %s|sudo -S %s' % (sudoPassword, bashCommand))
答案 1 :(得分:0)
如评论中所述,将线程设置为守护程序即可解决问题:
super().__init__(daemon=True)
程序退出时,守护进程线程将自动终止。
有关守护程序的更多信息: Daemon Threads Explanation