Python Subprocess module

Noah noah at
Sun Jul 15 19:14:31 CEST 2007

On Jul 13, 2:15 pm, Dave Sampson <sampe... at> wrote:
> hey folks,
> A simple question hopefully. despite all my searching I have not found a
> satisfactory response.
> The goal. Interact with a command line program. Simple enough, but the
> key is INTERACT.

You need Pexpect.

> So then I went with Popen and such... which then led to the subprocess
> module. I can create the object and read a few lines of output. but if I go too
> far then the program hangs. the number of lines will differ depandening
> on many function including the format of an input file so I can;t
> hardcode how many lines to read.

There is nothing you can do about this when using a pipe.
This is because stdio will change the type of buffering when sending
stdout and stderr to a pipe. From the client side there is no way you
can change the buffer mode (you CAN change the mode, but it only
it on YOUR side of the pipe -- not your child application's side).

> and nothing seems to get the program going again for I still cant; read
> past the same point in the standard output. then I have to kill and
> start over.

Your child application is waiting for you, but you can't see what it
becuase it has not filled or flushed the output buffer. When a program
write to stdout and it is connected to a TTY it will automatically
the output buffer when it writes a newline (or, rather, the clib will
do this).
But when writing to a pipe it will automatically use block buffering
will only flush the buffer when it is full.

It's not very complicated, but it can be very confusing at first.
It is also surprising that this behavior cannot be changed by the
parent application.

> So the next approach included looking atPexpect, which got realy
> confusing realy fast and despite running fedora core and python 2.4.4 I
> would like my application to be cross platform and there is noPexpect
> for Windows That I can see.

You are going to need something like Pexpect that uses pseudo-ttys
Pexpect works under Cygwin on Windows, but not under the native
Windows Python.

Email me if you have questions about Pexpect and I'll try to help you.


More information about the Python-list mailing list