[Python-Dev] Making popen2 actually work

Eric S. Raymond esr@golux.thyrsus.com
Tue, 14 Nov 2000 11:59:24 -0500


OK, so Guido told me at one point that 2.0 was supposed to have
included changes to the way buffering of I/O to subprocesses is
handled.  I've installed 2.0, and I need to know how to make the
following program work:

import os, sys

(child_stdin, child_stdout) = os.popen2("/usr/bin/rev", "t", 1);
print "* Child process spawned"

while 1:
    line = raw_input(">");
    child_stdin.write(line); child_stdin.flush()
    print "* Line written to child process"
    transformed = child_stdout.readline(); child_stdout.flush()
    print "* Line read back from child process."
    sys.stdout.write(transformed)

If anybody can tell me what the right magic is here, I'll write up 
a patch for the library docs explaining it and including the corrected
example.  This has to be a FAQ.
--
						>>esr>>