IPC

Donn Cave donn at u.washington.edu
Tue Jul 27 17:49:16 EDT 2004


In article <mailman.858.1090961212.5135.python-list at python.org>,
 Jp Calderone <exarkun at divmod.com> wrote:
> Donn Cave wrote:
...
> > Two points spring to mind:
> > 
> > 1.  Select's fundamental purpose, the reason you must
> >     write select([file], ... instead of select(file, ...
> >     is to handle multiple sources of input.
> > 
> > 2.  The file object's fundamental purpose is to make
> >     sure select doesn't work.  Even if output is line
> >     buffered, that doesn't help input, which will still
> >     confound select with process buffering.  The buffer
> >     parameters for the input file objects could do
> >     something, but at the cost of reading input byte
> >     by byte at considerable expense in system calls.
> >     Use file descriptors.
> 
>    I was with you up until the last sentence.  You seem to be implying that
> 
>      select([fileObj], ...)
> 
>    is different, at some underlying level, from
> 
>      select([fileObj.fileno()], ...)
> 
>    This is not the case.  If you pass a file object to select(), Python 
> will call fileno() for you.

Yes, that's true.  But do not then use the file object, like
for instance line = fileObj.readline(), or else you'll have
this problem.  The next select will see no data ready, even
though there likely are several lines worth that could be
retrieved via readline.

>    There is indeed buffering, but it will apply to the file descriptor 
> just as much as it will to the file object, because it happens at or 
> below the file descriptor level.

As long as it happens below the file descriptor level, it's
no problem because select functions at this level.  The file
object's problem is that it has process level buffers, thanks
to C library stdio, invisible to system I/O functions that
operate on file descriptors.

A more precise way to phrase my point might be `do not mix
system I/O functions with file object I/O functions.'  The
fact that select() accepts file objects does not make it a
file object function, rather it's a system I/O function.
It happens to know how to get the file descriptor from a
file object, but it doesn't know how to find out whether
the file object has buffered data, so the convenience is
somewhat questionable.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list