How to invoke subprocesses from Python

(Redirected from How to invoke sub-processes from Python)

by Nimish Pachapurkar

Edit

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.

Edit

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:

  1. A Popen class that allows very fine-grained control over the subprocess.
  2. Access to sub process's input, output and error streams.
  3. Ability to get the sub process read and write to and from already opened files.
  4. Ability to change current working directory.
  5. Ability to specify the newline characters used on the underlying OS.
  6. And most importantly, ability to do all of the above while constructing a Popen object.

Edit

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
MediaWiki

This page has been accessed 40,514 times.

This page was last modified 16:54, 27 April 2006.