Reading output from a child process non-blockingly

Thomas Guettler guettli at thomas-guettler.de
Wed Jun 29 15:45:20 CEST 2005


Am Wed, 29 Jun 2005 16:08:54 +0800 schrieb Yuan HOng:

> In my program I have to call an external program and parse its output.
> For that I use the os.popen2 function, and then read the output
> stream.
[cut]
> I tried use select.select on the output stream returned by os.popen2,
> but it returns a readable file descriptor only after the whole child
> process ends.

Select is the right module for this. But it only works on file descriptors
on unix.

What happens, if you run the external command on the shell like this:

ext_prog > out.log &

Check out.log with "tail" or "less +F". Do you see the data appear
in small chunks? If not, the application detects that stdout is not
a tty and you only get data if the buffer is full or if the application
ends.

If out.log gets filled in chunks you want to read, you can read this file
as long as the application is running. Remember the size of the file after
each read and use fd.seek() to read only the new data after opening it
again. This should work on windows, too.

HTH,
  Thomas

-- 
Thomas Güttler, http://www.thomas-guettler.de/





More information about the Python-list mailing list