Using popen interactively

Donn Cave donn at
Fri Jun 7 12:16:52 EDT 2002

Quoth Joe Mason <joe at>:
| So I'm trying to use popen2.Popen3("executable-name") to get handles to
| stdin and stdout.  Works fine, except that I'm not sure how to detect
| when the app has stopped printing and is waiting for input.  readline()
| gets the first lines ok, but then the ">" line has no newline on the
| end, so it blocks.  Apparently read() is supposed to return an empty
| string when it hits EOF, but it blocks too - so apparently on an
| interactive file stream like this EOF isn't set when its waiting for
| input.

Right.  I expect you'd do better to throw away the file objects
popen2 makes for you, and use the pipe file descriptors directly.
Instead of
    infile = app.fromchild
        line = infile.readline()
    infile = app.fromchild.fileno()
        data =, 8192)
        #  split into lines if you care

The stdio buffering in a file object is only trouble for you.
Part of this trouble is on the other end, if your external
process buffers output - in that case you'll have to figure
out how to use ptys.

	Donn Cave, donn at

More information about the Python-list mailing list