首页 > 解决方案 > 在子进程打开的长进程中获取 pid

问题描述

我需要在 Python 3.5.2 中使用 Popen 运行的进程中的 PID。

有这个:

with open(info['stdout_file'], 'w') as logfile:
    prcs = sp.Popen(split, 
                    stdout=logfile, 
                    stderr=logfile, 
                    cwd=info['runfolder'])

streams = prcs.communicate()
out, err = streams

post = {'pid': prcs.pid}
r = requests.post('https://.../receive_status.php', data=post)

只有在进程完成后才会发布进程的 PID。但是我需要它这个过程中才能杀死它。有什么办法吗?因为杀了

标签: python-3.xpopenpid

解决方案


pid 在 subprocess.Popen 返回对象后立即可用,无需等待。是等待进程完成的通信函数。


推荐阅读