readline() blocks after select() says there's data??

Grant Edwards grante at visi.com
Fri Mar 15 12:32:59 EST 2002


In article <mailman.1016212173.16075.python-list at python.org>, Rich Cook wrote:
> At 10:57 AM -0600 3/15/02, Skip Montanaro wrote:

>>     Rich> I don't really need select to block.  I just want it to be
>>     Rich> accurate.  If it returns a file object in the list of things which
>>     Rich> are available, why should then readline() block on it?
>>
>>Remember that select() is lower level than readline().  As
>>someone else pointed out, select() may return because there is
>>data available, but it makes no promises about what that data
>>is.  Calling readline() may then hang waiting for other
>>conditions (presence of a newline in the input, for example).
> 
> So do I have to read the data byte by byte using read(1) and
> examine each character to avoid blocking? Is there any way to
> find out how much data is actually waiting for me in the file
> object so I can read it in bigger increments, or must I call
> select then read(1) for every byte?

You don't have to read it 1 byte at a time. Just call
f.read(1024) and it will give you however many bytes are
available, up to the max of 1024.  You can use a max different
than 1024 if you want.

> The reason I'm concerned is that I have two file objects (childerr 
> and fromchild, actually) and I want to know right away if one has 
> data.  So I can't just call read on either one, because I may get 
> blocked on one waiting for data from the other.  But I'd like to 
> avoid the char-by-char read if possible.

-- 
Grant Edwards                   grante             Yow!  .. this must be what
                                  at               it's like to be a COLLEGE
                               visi.com            GRADUATE!!



More information about the Python-list mailing list