获得小孩过程的小孩 [英] obtaining pid of child process

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

问题描述

我正在使用python的多处理模块来生成新的流程

I am using python's multiprocessing module to spawn new process

如下:

import multiprocessing
import os
d = multiprocessing.Process(target=os.system,args=('iostat 2 > a.txt',))
d.start()

我想获取iostat命令的pid或使用多处理
模块执行的命令

I want to obtain pid of iostat command or the command executed using multiprocessing module

执行时:

 d.pid 

它给了我这个命令正在运行的subshel​​l。

it gives me pid of subshell in which this command is running .

任何帮助将是有价值的。

Any help will be valuable .

提前感谢

推荐答案

p>与@rakslice类似,您可以使用 psutil

Similar to @rakslice, you can use psutil:

import signal, psutil
def kill_child_processes(parent_pid, sig=signal.SIGTERM):
    try:
      parent = psutil.Process(parent_pid)
    except psutil.NoSuchProcess:
      return
    children = parent.children(recursive=True)
    for process in children:
      process.send_signal(sig)

这篇关于获得小孩过程的小孩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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