我有一个subprocess
进程列表,我不与它们通信,只是等待。
我希望等待第一个进程完成(这个解决方案有效):
import subprocess
a = subprocess.Popen(['...'])
b = subprocess.Popen(['...'])
# wait for the first process to finish
while True:
over = False
for child in {a, b}:
try:
rst = child.wait(timeout=5)
except subprocess.TimeoutExpired:
continue # this subprocess is still running
if rst is not None: # subprocess is no more running
over = True
break # If either subprocess exits, so do we.
if over:
break
我不想使用os.wait()
,因为它可能会从另一个不在我等待列表中的subprocess
返回。
一种优雅的解决方案可能是使用epoll
或select而不需要任何循环。
.communicate
? - Chase