popen question

Hrvoje Niksic hniksic at xemacs.org
Tue Jan 8 04:59:53 EST 2008


Robert Latest <boblatest at yahoo.com> writes:

> If 'slow' or some other program does buffered output, how come I can
> see its output line-by-line in the shell?

stdio uses different buffering strategies depending on the output
type.  When the output is a TTY, line buffering is used; when the
output goes to a pipe or file, it is fully buffered.

> In reality I want to call another program whose behavior I can't
> influence (well, technically I could because it's open-source, but
> let's assume it to be a black box for now).

To test whether your black box buffers output to pipe, simply start it
like this:

$ ./slow | cat

If you see lines one by one, you are in luck, and you can fix things
on the Python level simply by avoiding buffering in popen.  If not,
you will need to resort to more advanced hackery (e.g. fixing stdio
using LD_PRELOAD).



More information about the Python-list mailing list