python-3.x - 杀死python3.X中的所有进程和线程

标签 python-3.x multithreading ubuntu kill

我正在编写一个 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,我不关心跨平台功能,但它们会很好

最佳答案

首先导入os库:

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)) 

关于python-3.x - 杀死python3.X中的所有进程和线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62873234/

相关文章:

asp.net-core - 无法使用 nginx 访问托管在 ubuntu 上的网站

python - 尝试部署 django 时出现静态文件问题

python - 在 lambda 中自动元组解包的好的 python3 等价物是什么?

python - 按多列将 csv 文件拆分为 Pandas 数据框

performance - 有人用过谷歌性能工具吗?

c++ - 一些boost线程问题C++

python - PyPy 和 Python 版本

Python - 仅向列表添加一次

python - 如何使用 Python 获取某人的 Facebook ID?

python-3.x - python并行读取csv文件并连接数据框