Recipe9.11.Driving an External Process with popen


Recipe 9.11. Driving an External Process with popen

Credit: Sébastien Keim, Tino Lange, Noah Spurrier

Problem

You want to drive an external process that accepts commands from its standard input, and you don't care about the responses (if any) that the external process may emit on its standard output.

Solution

If you need to drive only the other process' input and don't care about its output, the simple os.popen function is enough. For example, here is a way to do animated graphics by driving the free program gnuplot via os.popen:

import os f = os.popen('gnuplot', 'w') print >>f, "set yrange[-300:+300]" for n in range(300):     print >>f, "plot %i*cos(x)+%i*log(x+10)" % (n, 150-n)     f.flush( ) f.close( )

Discussion

When you want to use Python as a glue language, sometimes (in particularly easy cases) the simple function popen (from the standard library module os) may be all you need. Specifically, os.popen may suffice when you need to drive an external program that accepts commands on its standard input, as long as you can ignore any response that the program might be making on its standard output (and also error messages that the program might be sending to its standard error). A good example is given by the free plotting program gnuplot. (os.popen may also suffice when you need to obtain the output from a program that does not need to read its standard input.)

The statement f = os.popen('gnuplot', 'w') creates a file-like object connected to the standard input of the program it launches, namely 'gnuplot'. (To try this recipe, you have to have gnuplot installed on your PATH, but since gnuplot is freely available and widely ported software, that should not be a problem!) Whatever we write to f, the external process receives on its standard input, just as would happen if we used that same program interactively. For more of the same, check out http://sourceforge.net/projects/gnuplot-py/: it's a rich and interesting Python interface to gnuplot implemented entirely on the basis of the simple idea shown in this recipe!

When your needs are more sophisticated than os.popen can accommodate, you may want to look at os.popen2 and other such higher-numbered functions in module os, or, in Python 2.4, the new standard library module subprocess. However, in many cases, you're likely to be disappointed: as soon as you get beyond the basics, driving (from your own programs) other external programs that were designed to be used interactively can become more than a little frustrating. Fortunately, a solution is at hand: it's pexpect, a third-party Python module that you can find at http://pexpect.sourceforge.net/. pexpect is designed specifically for the task of driving other programs, and it lets you check on the other program's responses as well as sending commands to the other program's standard input. Still, while pexpect will most definitely offer you all the power you need, os.popen will probably suffice when you don't need anything fancy!

See Also

Module os (specifically os.popen) in the Library Reference and Python in a Nutshell; gnuplot is at http://www.gnuplot.info/; gnuplot.py is at http://sourceforge.net/projects/gnuplot-py/; pexpect is at http://pexpect.sourceforge.net/.



Python Cookbook
Python Cookbook
ISBN: 0596007973
EAN: 2147483647
Year: 2004
Pages: 420

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net