Re: os.select misbehavior with pipe

Sjoerd Mullender (Sjoerd.Mullender@cwi.nl)
Tue, 10 Jan 1995 14:48:19 +0100

On Tue, Jan 10 1995 Ken Manheimer wrote:

> I am perplexed.
>
> Since a python version of 'expect' is not immediately forthcoming, as
> yet, i'm giving in and putting together a 'subprocess' class, based on
> one posted a while back by jose pereira. I'm having a weird problem
> with using 'select' to poll the pipes connecting to the subprocess. I
> put together the following little module, 'prob.py', in order to
> exhibit the problem in as succinct a way as i could muster.
>
> I'm no expert on using select, so i can't be very confident that i'm
> using it properly. However, the python interface to it, in
> particular, appears fairly simple, and i suspect my problem has to do
> with the select implementation. Still, i would not be surprised if
> i'm misapprehending something, and corrections are quite welcome.
>
> -=- -=V=- -=-
> """prob.py - succinct example demonstrating pipe IO select problem.
> ken.manheimer@nist.gov, 10-Jan-1995."""
>
> import sys, os, select
>
> rf, wf = os.pipe()
> rfp = os.fdopen(rf, 'r'); wfp = os.fdopen(wf, 'w')
>
> wfp.write('the\n'); wfp.write('spanish\n'); wfp.write('inquisition\n')
> wfp.flush()
> print 'Pipe primed with three lines written.'
>
> for i in range(3):
> print 'Select on read handle:', select.select([rfp], [],[], 0)[0]
> print '... and read yeilds:', rfp.readline()[:-1]
> -=- -=^=- -=-
>
> What i believe *ought* to be happening in the loop is that the select
> should be returning the read file-descriptor every time through, but
> it only does so before the first read. All the reads, even those
> following empty select returns, each yield one of the three lines with
> which the pipe was primed.

This is not what *ought* to happen. Select works on file descriptors
and returns any descriptors that have unread data. The readline
method uses stdio for its implementation, and stdio buffers. So after
the first readline all data has been read from the pipe and is stored
in an internal buffer from which the second and third line are
returned in subsequent calls. So there is nothing left in the pipe
after the first readline, and thus select does not return the an
indication that there is something.

> Here is a transcript of a run:
>
> -=- -=V=- -=-
> >>> import prob
> Pipe primed with three lines written.
> Select on read handle: [<open file '(fdopen)', mode 'r' at 1469b0>]
> ... and read yeilds: the
> Select on read handle: []
> ... and read yeilds: spanish
> Select on read handle: []
> ... and read yeilds: inquisition
> <module 'prob'>
> >>>
> -=- -=^=- -=-
>
> This occurs on my solaris 1.1.1 and 2.3 systems, both running the same
> versions of python 1.1.1 compiled under solaris 1.1.1, both with and
> without guido's import mods.
>
> Anyone have any clues as to what may be happening here???
>
> Thanks,
>
> Ken
> ken.manheimer@nist.gov, 301 975-3539

Sjoerd Mullender, CWI, P.O. Box 94079, 1090 GB Amsterdam, The Netherlands
E-Mail: Sjoerd.Mullender@cwi.nl; Phone: +31 20 592 4127; Fax: +31 20 592 4199
URL: http://www.cwi.nl/cwi/people/Sjoerd.Mullender.html