Python on Windows - how to wait for multiple child processes?

前端 未结 6 1378
滥情空心
滥情空心 2020-12-05 18:12

How to wait for multiple child processes in Python on Windows, without active wait (polling)? Something like this almost works for me:

proc1 = subpr         


        
6条回答
  •  情话喂你
    2020-12-05 18:51

    It might seem overkill, but, here it goes:

    import Queue, thread, subprocess
    
    results= Queue.Queue()
    def process_waiter(popen, description, que):
        try: popen.wait()
        finally: que.put( (description, popen.returncode) )
    process_count= 0
    
    proc1= subprocess.Popen( ['python', 'mytest.py'] )
    thread.start_new_thread(process_waiter,
        (proc1, "1 finished", results))
    process_count+= 1
    
    proc2= subprocess.Popen( ['python', 'mytest.py'] )
    thread.start_new_thread(process_waiter,
        (proc2, "2 finished", results))
    process_count+= 1
    
    # etc
    
    while process_count > 0:
        description, rc= results.get()
        print "job", description, "ended with rc =", rc
        process_count-= 1
    

提交回复
热议问题