Best way to launch a process and block until it is finished
I'm told subprocess.Popen does not b开发者_开发技巧lock, so the process runs in the background and your python program does not wait until it has finished. However, this is not what I want.
I'm having my program call into an external python program, which I have no control over. It does not return any return codes, just runs, operates on files, and finishes. I want to call this and only continue my python program when that call has completed. How do I do this?
Call communicate or wait on the subprocess.
Use the call
method from subprocess.
subprocess.call(*popenargs, **kwargs) Run command with arguments. Wait for command to complete, then return the returncode attribute.
see → http://docs.python.org/library/subprocess.html#subprocess.call
Best way is to use the communicate
method:
http://docs.python.org/library/subprocess.html#subprocess.Popen.communicate
Also have a look at the wait
method, it states why most of the time you should use communicate instead:
http://docs.python.org/library/subprocess.html#subprocess.Popen.wait
精彩评论