Capturing stdout without waiting for the process end

Donn Cave donn at u.washington.edu
Mon Apr 3 15:24:39 EDT 2006


In article <1144083372.170752.275520 at z34g2000cwc.googlegroups.com>,
 "Luigi" <luigipaioro at libero.it> wrote:

> The question is that I have a C program (by third part) that streams
> the logs into the stderr and stdout devices. I need to create an
> envelopment that captures the outputs and puts them in a file
> generating log events (for a real-time view).

As suggested in another followup, the C program's
output will probably be "block buffered" when its
output is a pipe.  In this case, you'll get output
only when the buffer is full, or when the program
exits.

If the program can be modified, it only needs to
flush stdout after each output event.  Otherwise,
you need to use a device that looks like a tty,
just so the C stdio library will switch to line
buffering as it generally does with terminals.

This is called a pseudotty.  It's a little more
difficult to use than a pipe, but you can probably
get something going with openpty or forkpty from
the os/posix module, or there may still be 3rd
party packages for this.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list