Socket Troubles

Chris Spencer usenet.20.evilspam at spamgourmet.com
Sun Aug 28 15:09:19 EDT 2005


Michael Sparks wrote:
> Chris Spencer wrote:
> 
> 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! :)

You're quite right. I fixed this by using select(). However, I was still 
having problems with open() blocking the main thread. Then I realized a 
slight problem:
         t = threading.Thread(target=self._connection_handler(h))

I changed this to:
         t = threading.Thread(target=self._connection_handler, args=(h,))
and now it appears to be working correctly.

Thanks for your help! I truly appreciate it.

Chris



More information about the Python-list mailing list