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

Chris McDonough chrism at plope.com
Thu Aug 5 16:04:56 CEST 2004


On Thu, 2004-08-05 at 01:50, Greg Ewing wrote:
> 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.

Of course!  Thanks.  Let us speak no more of this. ;-)

- C




More information about the Python-Dev mailing list