Working around buffering issues when writing to pipes
mdw at distorted.org.uk
Tue Apr 22 14:42:00 CEST 2008
sven _ <svensven at gmail.com> wrote:
> In short: unless specifically told not to, normal C stdio will use
> full output buffering when connected to a pipe. It will use default
> (typically unbuffered) output when connected to a tty/pty.
Wrong. Standard output to a terminal is typically line-buffered.
(Standard error is never buffered by default, per the ISO C standard.)
> This is why subprocess.Popen() won't work with the following program
> when stdout and stderr are pipes:
> I went with pexpect and it works well, except that I must handle
> stdout and stderr separately. There seems to be no way to do this with
> pexpect, or am I mistaken?
You could do it by changing the command you pass to pexpect.spawn so
that it redirects its stderr to (say) a pipe. Doing this is messy,
though -- you'd probably need to set the pipe's write-end fd in an
environment variable or something.
It's probably better to use os.pipe and pty.fork.
As mentioned above, the inferior process's output will still be
line-buffered unless it does something special to change this.
More information about the Python-list