popen in real time?

Donn Cave donn at oz.net
Wed Jun 7 01:01:18 EDT 2000


Quoth msoulier at nortelnetworks.com (Michael P. Soulier):
| 	Hey guys. I realize that you can loop on the output of popen if you want
| to read from a pipe, but it would seem from experience that the output of the
| child process is not available until the process exits. I've tried this with
| the output of long-running processes like find. Is there a way to get the
| output of a child in real time, as it's printed?

If it's like everything else, find's output is C library stdio,
like fputs() and so forth.  This is a buffered system, where the
real device I/O (like write()) happen when the buffer fills up.
That's where your output is, waiting in find's process memory,
and there isn't a heck of a lot you can do about it.  (Assuming
you can't modify find.)

There is a significant exception, though, inasmuch as when the
output device is a tty, stdio doesn't buffer output (or doesn't
block buffer it, anyway.)  UNIX provides a pipe-like device with
tty driver interface, called a pseudo-tty or pty, and if you're
really determined to get this to work, that's what you have to
work with.  There's a terrific utility for this called "Expect",
a Tcl extension, that knows how to deal with the pty devices of
many different platforms.  Python has a pty module in its standard
distribution, albeit able to deal with a much narrower range of
platforms, and there are some contributed modules that support
expect-like functions but I don't know much about them.

	Donn Cave, donn at oz.net



More information about the Python-list mailing list