zseil의 답변을 바탕으로 작성하면 하위 프로세스와 win32 API 호출을 혼합하여이 작업을 수행 할 수 있습니다. 필자는 파이썬이 win32api를 설치하지 않았기 때문에 직선의 ctypes를 사용했습니다. 난 그냥 여기에 예를 들어, MSYS에서 sleep.exe를 산란거야하지만 분명히 당신이 좋아하는 모든 프로세스를 산란 수 있습니다. OpenProcess()를 사용하여 프로세스의 PID에서 핸들을 가져온 다음 WaitForMultipleObjects를 사용하여 프로세스가 완료 될 때까지 기다립니다.
import ctypes, subprocess
from random import randint
SYNCHRONIZE=0x00100000
INFINITE = -1
numprocs = 5
handles = {}
for i in xrange(numprocs):
sleeptime = randint(5,10)
p = subprocess.Popen([r"c:\msys\1.0\bin\sleep.exe", str(sleeptime)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
h = ctypes.windll.kernel32.OpenProcess(SYNCHRONIZE, False, p.pid)
handles[h] = p.pid
print "Spawned Process %d" % p.pid
while len(handles) > 0:
print "Waiting for %d children..." % len(handles)
arrtype = ctypes.c_long * len(handles)
handle_array = arrtype(*handles.keys())
ret = ctypes.windll.kernel32.WaitForMultipleObjects(len(handle_array), handle_array, False, INFINITE)
h = handle_array[ret]
ctypes.windll.kernel32.CloseHandle(h)
print "Process %d done" % handles[h]
del handles[h]
print "All done!"
당신은 waitpid를 (0) 유닉스에서 사용하는 것을 설명 할 수 있습니다
선택적으로 당신은 감시 프로세스 중 하나가 종료 될 때마다 호출됩니다 콜백을 지정할 수 있습니다? –
http://docs.python.org/library/os.html#os.waitpid 유닉스에서'waitpid (0)'은 사용 가능한 자식 상태에 대해 대기하고 ('WNOHANG'이 옵션에없는 경우) 기다리고'(, 상태)'튜플. –