监视python中子进程生成的进程数

时间:2014-06-02 21:33:44

标签: python python-3.x process

我希望能够做到这样的事情:

import subprocess
nproc = 0
for i in range(100):
    subprocess.Popen(commands[i], when_finished="nproc -= 1")
    nproc += 1

while nproc > 0:
    print("%d processes running..." % nproc)

1 个答案:

答案 0 :(得分:2)

轮询流程:

import subprocess
processes = [subprocess.Popen(command) for command in commands]
while True:
    nproc = sum(p.poll() is None for p in processes)
    if not nproc:
        break
    print("%d processes running..." % nproc)
    sleep(0.1)