开发者

Best way to launch a process and block until it is finished

开发者 https://www.devze.com 2023-03-27 23:35 出处:网络
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 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

0

精彩评论

暂无评论...
验证码 换一张
取 消