Page Tools:
Wiki Relationships:
Admin Tools:
How to invoke subprocesses from Python
by Nimish Pachapurkar
Introduction
The "os" module that traditionally shipped with Python distributions had some support for process management. The major drawbacks in this implementation was that there was no way to get the exit code of the process and also open pipes to its input, output and error streams. The Popen3 and Popen4 classes provided a mechanism which was not available on Windows.
subprocess Module
Python 2.4 ships with a module called subprocess that allows much better control over process management. The useful features of this module include:
- A Popen class that allows very fine-grained control over the subprocess.
- Access to sub process's input, output and error streams.
- Ability to get the sub process read and write to and from already opened files.
- Ability to change current working directory.
- Ability to specify the newline characters used on the underlying OS.
- And most importantly, ability to do all of the above while constructing a Popen object.
Convenience Method
The subprocess module also provides a very handy convenience function called "call()". Call takes a list as a command line and all the remaining options that Popens contructor takes. Please see the module documentation from the related links. This function waits for the subprocess to exit and return a numeric exit code that can be checked to assert the success for the subprocess. Here is a sample code that executes a long command line and gets the exit code:
#!/opt/oss/bin/python
import os, subprocess
# Create a long command line
cmd = [\
"mysqldump", \
"--defaults-extra-file=/opt/oss/etc/mysql/my.pwd",\
"--lock-all-tables",\
"--complete-insert",\
"--quote-names",\
"--add-locks",\
"--disable-keys",\
"--add-drop-database",\
"--add-drop-table",\
"--databases",\
"employee"\
]
# Create output log file
outFile = os.path.join(os.curdir, "output.log")
outptr = file(outFile, "w")
# Create error log file
errFile = os.path.join(os.curdir, "error.log")
errptr = file(errFile, "w")
# Call the subprocess using convenience method
retval = subprocess.call(cmd, 0, None, None, outptr, errptr)
# Close log handles
errptr.close()
outptr.close()
# Check the process exit code
if not retval == 0:
errptr = file(errFile, "r")
errData = errptr.read()
errptr.close()
raise Exception("Error executing command: " + repr(errData))
Most Recent |
Most Popular |
Most Active Categories |
| Back To Top | Add New Article | Printable Page |

Testing
