Select hangs after some reads

Steve Holden steve at
Thu Jun 8 05:52:26 CEST 2006

alsmeirelles at wrote:
> Hi,
> I'm building a multithreaded application and I encountered a tiny and
> annoying problem. I use a select to wait for data to be read from a
> socket, after some reads, the select simply blocks and stays that way
> until I close the connection on the other side of the socket. When the
> socket is closed on the writer end the select releases and then I get
> only empty strings from the socket.

That's to be expected: the first return of a zero-length string from indicates end of file.

> My question is this: Why did it block? The reading has never ended,
> every test I make I write 50 requests (wich are strings) to the socket
> and I have read the maximum of 34 requests. I'm using winPdb to take a
> closer look on what's happening and I see the threads blocked on this
> same select. If I send anything more through the socket, the select
> releases for a thread, despite the other data that is still unread.
Are you sure that the received data is presenting in block the same size 
as are being sent? There's no guarantee this will be so on a TCP socket, 
and it may be that multiple sends are being coalesced into a single 
read. The important thing to focus on is the total number of bytes sent 
and received.

> This is the select:
>  rd,w,e =[self.rfd],[],[])
> self.rfd is the fileno of the file object returned by the makefile
> method from the socket object.
It would be simpler to use the result of the socket's .fileno() method 

> I know that's some buffer behavior that I'm missing but I don't know
> what it is.
> anything is helpfull, If I'm beeing stupid you can say it.
> thanks
Without being able to see all your code it's hard to say whether or not 
you are doing something daft, but you give a general impression of 

Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd
Love me, love my blog
Recent Ramblings

More information about the Python-list mailing list