wait process until all subprocess finish?

Question:

I have a main process which creates two or more sub processes, I want main process to wait until all sub processes finish their operations and exits?

 # main_script.py

 p1 = subprocess.Popen(['python script1.py']) 
 p2 = subprocess.Popen(['python script2.py'])
 ... 
 #wait main process until both p1, p2 finish
 ...
Asked By: Nikhil Rupanawar

||

Answers:

subprocess.call

Automatically waits , you can also use:

p1.wait()
Answered By: Gjordis

A Popen object has a .wait() method exactly defined for this: to wait for the completion of a given subprocess (and, besides, for retuning its exit status).

If you use this method, you’ll prevent that the process zombies are lying around for too long.

(Alternatively, you can use subprocess.call() or subprocess.check_call() for calling and waiting. If you don’t need IO with the process, that might be enough. But probably this is not an option, because your if the two subprocesses seem to be supposed to run in parallel, which they won’t with (call()/check_call().)

If you have several subprocesses to wait for, you can do

exit_codes = [p.wait() for p in p1, p2]

(or maybe exit_codes = [p.wait() for p in (p1, p2)] for syntactical reasons)

which returns as soon as all subprocesses have finished. You then have a list of return codes which you maybe can evaluate.

Answered By: glglgl
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.