你如何在python中列出所有子进程?

问题描述 投票:14回答:3

我正在使用启动各种子流程的第三方库。当有异常时我想杀死所有子进程。我如何获得儿童pids列表?

python subprocess
3个回答
18
投票

您无法始终在创建所有子流程时对其进行记录,因为它们可以反过来创建您不知道的新流程。但是,使用psutil来查找它们非常简单:

import psutil

current_process = psutil.Process()
children = current_process.children(recursive=True)
for child in children:
    print('Child pid is {}'.format(child.pid))

2
投票

在创建子进程时,记录所有子进程的pid通常更安全。没有符合posix的方法来列出子PID。我知道这可以用PS工具完成。


1
投票

使用psutil你可以让所有的孩子进程(甚至是递归过程)看看https://psutil.readthedocs.io/en/latest/#psutil.Process.children

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.