Dialog with a process via subprocess.Popen blocks forever
Bernhard Herzog
bh at intevation.de
Fri Mar 2 12:31:43 EST 2007
bayer.justin at googlemail.com writes:
> So, once I start the C Program from the shell, I immediately get its
> output in my terminal. If I start it from a subprocess in python and
> use python's sys.stdin/sys.stdout as the subprocess' stdout/stdin I
> also get it immediately.
If stdout is connected to a terminal, it's usually line buffered, so the
buffer is flushed whenever a newline is written.
> BUT If I use PIPE for both (so I can .write() on the stdin and .read()
> from the subprocess' stdout stream (better: file descriptor)) reading
> from the subprocess stdout blocks forever. If I write something onto
> the subprocess' stdin that causes it to somehow proceed, I can read
> from its stdout.
When stdout is not connected to a terminal, it's usually fully buffered,
so that nothing is actually written to the file until the buffer
overflows or until it's explictly flushed.
If you can modify the C program, you could force its stdout stream to be
line buffered. Alternatively, you could call fflush on stdout whenever
you're about to read from stdin. If you can't modify the C program you
may have to resort to e.g. pseudo ttys to trick it into believing that
its stdout is a terminal.
Bernhard
--
Intevation GmbH http://intevation.de/
Skencil http://skencil.org/
Thuban http://thuban.intevation.org/
More information about the Python-list
mailing list