Select hangs after some reads

alsmeirelles at gmail.com alsmeirelles at gmail.com
Fri Jun 9 09:37:18 EDT 2006


Grant Edwards escreveu:

> On 2006-06-08, alsmeirelles at gmail.com <alsmeirelles at gmail.com> wrote:
>
> >>> Well, actually I´m using a very simple protocol wich sends
> >>> only strings ended by newline. I need to send 3 chunks of
> >>> information and a newline after them. On the reader side I
> >>> make 3 readline(), this way I wouldn´t have to care about this
> >>> problem, but maybe that´s where I´m falling. If that´s the
> >>> case, I´ll have to use a more complex protocol.
> >>
> >> You can't use readline() with select().  Select tells you
> >> whether recv() called on the underlying socket will block or
> >> not.  What's probably happening is that all of the data has
> >> been read from the underlying socket and is being held in a
> >> buffer waiting to be read by readline().
> >
> > Yes, as I expected, its the buffers. In my opinion the problem
> > is that the socket module doesn't provide a way of reading all
> > its internal buffer.
>
> What internal buffer?
>
> > readlines() just make subsequent calls to readline and
> > readline may call recv, so we have a locked scene. I want to
> > know if I will block anyway.
>
> I'm lost.
>
> > Of course I can clean the buffer myself, but I think the
> > socket module should provide a way of doing this.
>
> I'm afraid I don't understand what you mean.
>
> Since you talked about calling readline(), I assumed that you
> had called the socket object's makefile() method to create a
> file-object.  The select system call can only tell you whether
> a recv on the socket will block or not.  It knows nothing about
> the state of the file object on which you're calling readline().


Yes, that's right. The problem I was having and discussing is that, if
you read one request at a time,
using a readline, and used select to check for the incoming of new
ones, you will block and the other
unread requests will rest in the socket buffer (_rbuf class attribute,
wich is a string). If you use several readline() (that's readlines()),
to try to read all the buffer, and use select to check for the new
ones, you will block because after the buffer is empty readline() will
try a recv(). So the problem: readline() and readlines() can block and
there's nothing (given by the module) to prevent you from it happening.

There are solutions to this of course, but what I was arguing is that
the module and the documentation don't warn and dont treat this case.

>
> --
> Grant Edwards                   grante             Yow!  Is this an out-take
>                                   at               from the "BRADY BUNCH"?
>                                visi.com




More information about the Python-list mailing list