poll() on a linux box always returns POLLIN

brueckd at tbye.com brueckd at tbye.com
Tue Nov 27 15:22:00 CET 2001


On Tue, 27 Nov 2001, ab wrote:

> Just trying out some asynchronous socket operations in python,
> simple client server test code.
>
> Anyway so I get a queue of threads going waiting to be passed sockets.
> When they get a socket, I am using poll to implement timeouts. However
> poll() always seems to return data ready POLLIN, even when client has
> disconnected and finished. when reading the socket, 0 bytes of data is
> returned, as you would expect with the peer disconnected ???
> any thoughts ???

When recv returns an empty string, it means that the connection is done,
there's no more data, so it's time to quit. For example, the code to read
all data from a socket synchronously looks like this:

while 1:
  data = sock.recv(4096)
  if not data: break
  # do something with the data

With asynch sockets it's about the same; your code that handles POLLIN
should call recv and, if the string returned is empty, do whatever you
need to do to handle the end of the connection (most likely remove your
socket from the poller and close it).

-Dave





More information about the Python-list mailing list