Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wait process until all subprocess finish? [duplicate]

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  ... 
like image 971
Nikhil Rupanawar Avatar asked Feb 27 '13 08:02

Nikhil Rupanawar


People also ask

How do you wait for a subprocess to finish?

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.

Does subprocess run wait for finish?

subprocess. run() is synchronous which means that the system will wait till it finishes before moving on to the next command.

Does subprocess Popen run in parallel?

We can use subprocess module to create multiple child processes and they are run in parallel.

What is subprocess popen ()?

The subprocess. popen() is one of the most useful methods which is used to create a process. This process can be used to run a command or execute binary. The process creation is also called as spawning a new process which is different from the current process.


1 Answers

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] 

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

like image 130
glglgl Avatar answered Oct 02 '22 05:10

glglgl