Python: waiting for external launched process finish Python: waiting for external launched process finish python python

Python: waiting for external launched process finish


Use subprocess instead:

import subprocessfor i in xrange(n):  p = subprocess.Popen(('someprog.exe', str(i))  p.wait()

Read more here: http://docs.python.org/library/subprocess.html


os.system() does wait for its process to complete before returning.

If you are seeing it not wait, the process you are launching is likely detaching itself to run in the background in which case the subprocess.Popen + wait example Dor gave won't help.

Side note: If all you want is subprocess.Popen + wait use subprocess.call:

import subprocesssubprocess.call(('someprog.exe', str(i)))

That is really no different than os.system() other than explicitly passing the command and arguments in instead of handing it over as a single string.