我希望能够做到这样的事情:
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)
答案 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)