获取递归子流程的pid [英] Get pid of recursive subprocesses

查看:181
本文介绍了获取递归子流程的pid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:子流程创建了一个子流程,依此类推,如何获取它的pid?

Scenario: subprocess created a subprocess and so on, how can i get it's pid?

我使用subprocess.popen启动了第一个子进程,例如word文件,这个word文件生成了一个新的子进程,我如何获取它的pid?

I used subprocess.popen to launch the first subprocess, for example word file, this word file generated a new subprocess, how can i get it's pid?

推荐答案

使用psutil:

parent = psutil.Process(parent_pid)
children = parent.children()
# all child pids can be accessed using the pid attribute
child_pids = [p.pid for p in children]

这篇关于获取递归子流程的pid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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