Using popen interactively

Donn Cave donn at u.washington.edu
Fri Jun 7 18:16:52 CEST 2002


Quoth Joe Mason <joe at notcharles.ca>:
...
| 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()
say
    infile = app.fromchild.fileno()
    ...
        data = posix.read(infile, 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 u.washington.edu



More information about the Python-list mailing list