Problems with select

Andrae Muys amuys at shortech.com.au
Wed Feb 27 21:07:40 EST 2002


Alain Tesio <alain at onesite.org> wrote in message news:<pan.2002.02.27.01.03.24.922015.11917 at onesite.org>...
> Hi, thanks for the explanation, but select still returns files where
> there is nothing to read, after the end of the stream.
> 
<SNIP to output>
> 4 select returns :  ([<open file '(fdopen)', mode 'r' at 0x8060450>, <open file '(fdopen)', mode 'r' at 0x808d510>], [], [])
> something on out : ''
> something on err : ''

Quoting from the Python Library Reference "2.1.8.9 File Objects"

readline([size]) 
Read one entire line from the file. A trailing newline character is
kept in the string2.7 (but may be absent when a file ends with an
incomplete line). If the size argument is present and non-negative, it
is a maximum byte count (including the trailing newline) and an
incomplete line may be returned. An empty string is returned when EOF
is hit immediately. Note: Unlike stdio's fgets(), the returned string
contains null characters ('\0') if they occurred in the input.

So that's why you're getting an empty string, ie. EOF.  Quoting
again...

read([size]) 
Read at most size bytes from the file (less if the read hits EOF
before obtaining size bytes). If the size argument is negative or
omitted, read all data until EOF is reached. The bytes are returned as
a string object. An empty string is returned when EOF is encountered
immediately. (For certain files, like ttys, it makes sense to continue
reading after an EOF is hit.) Note that this method may call the
underlying C function fread() more than once in an effort to acquire
as close to size bytes as possible.

Notice the comment regarding tty's.  This is the reason select keeps
returning.  You can always read an EOF, so select returns to let you
read it.
You should probably close() the file once you it EOF, and remove it
from the select call.

Andrae Muys



More information about the Python-list mailing list