Using popen interactively
kragen at pobox.com
Sun Jun 9 04:43:36 CEST 2002
[note Joe is talking about a game by a guy named Don about a cave, or
a derivative thereof]
"Donn Cave" <donn at u.washington.edu> writes:
> 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()
> 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 = posix.read(infile, 8192)
But not too soon --- if the program does multiple writes, this might
return before the last of them.
> # 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