select() vs. pipes (was [Python-Dev] Re: PEP 324 (process module))

Greg Ewing greg at cosc.canterbury.ac.nz
Thu Aug 5 07:50:37 CEST 2004


Chris McDonough <chrism at plope.com>:

> The following program demonstrates that a particular usage of select
> (under Linux at least) always returns the output side of a pipe
> connected to a child process' stdout as "ready" after it gets any output
> from that child process, even if the child process has no further data
> to provide after it has provided a bit of data to the parent.
>
> Or am I doing something silly?
> 
>         for fd in r:
>             sys.stdout.write(os.read(fd, 1024))

You're not taking account of the possibility of EOF. When
the child process finishes and closes its end of the pipe,
the select will always report the pipe as ready for reading,
because it is -- subsequent reads will return immediately
with 0 bytes.

You need to check whether the read returned 0 bytes, and
take that as meaning that the child process has finished.

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg at cosc.canterbury.ac.nz	   +--------------------------------------+


More information about the Python-Dev mailing list