Socket Troubles

Michael Sparks ms at cerenity.org
Sun Aug 28 12:02:34 EDT 2005


Chris Spencer wrote:
> My code's ... at http://deadbeefbabe.org/paste/1525/0
...
> I've written a simple class to manage P2P socket connections. However,
> whenever I try to receive data, the socket raises an exception with the
> error message  (11, 'Resource temporarily unavailable').

At one point in your code you do this: 
        self._socket.setblocking(0)

This says "if we can't recieve or send data without blocking, fail rather 
than succeed". One of the failure modes is to return error 11. This is 
infact normally defined as:
#define EAGAIN          11      /* Try again */

What this means is "sorry, I couldn't do this without blocking right now, 
try again very shortly!".

Looking at your code it continually loops (in BaseConnectionHandler_recv) 
receiving data - whether or not there's any data to receive:
    def _recv(self):
        while self.running:
            try:
                data = self._socket.recv(4096)
                if not len(data):
                    time.sleep(0.1)
                    continue
            except Exception, e:
                log('Recieve failed for handler',self.address,'because of',e)

                break

Since you never actually check to see if the socket is ready to give you
data, and you've set it non-blocking, seeing lots of EAGAIN errors is
pretty much what you'd expect to see. (Simply sleeping is not sufficient!)

I suppose the short answer though really is this: you set the socket
non-blocking, you should therefore expect to see failures telling you
to try again, and follow their advice! :)

Regards,


Michael.




More information about the Python-list mailing list