Waiting for a subprocess to exit

Ben Finney ben+python at benfinney.id.au
Fri Aug 21 01:13:56 EDT 2009


Howdy all,

I'm looking to replace some usages of ‘os.system’ with the more secure
‘subprocess.Popen’ methods.

The module documentation has a section on replacing ‘os.system’
<http://docs.python.org/library/subprocess#replacing-os-system>, which
says to use::

    process = subprocess.Popen("mycmd" + " myarg", shell=True)
    status = os.waitpid(process.pid, 0)

But a ‘Popen’ instance has its own ‘wait’ method, which waits for exit
<URL:http://docs.python.org/library/subprocess#subprocess.Popen.wait>.
Why would I use ‘os.waitpid’ instead of::

    process = subprocess.Popen("mycmd" + " myarg", shell=True)
    process.wait()
    status = process.returncode

-- 
 \       “The best is the enemy of the good.” —Voltaire, _Dictionnaire |
  `\                                                    Philosophique_ |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list