In Python epoll can I avoid the errno.EWOULDBLOCK, errno.EAGAIN ?

Posted by davyzhang on Stack Overflow See other posts from Stack Overflow or by davyzhang
Published on 2011-06-22T08:05:04Z Indexed on 2011/06/22 16:22 UTC
Read the original article Hit count: 585

Filed under:
|
|

I wrote a epoll wrapper in python, It works fine but recently I found the performance is not not ideal for large package sending. I look down into the code and found there's actually a LOT of error

Traceback (most recent call last):
  File "/Users/dawn/Documents/workspace/work/dev/server/sandbox/single_point/tcp_epoll.py", line 231, in send_now
    num_bytes = self.sock.send(self.response)
error: [Errno 35] Resource temporarily unavailable

and previously silent it as the document said, so my sending function was done this way:

def send_now(self):
    '''send message at once'''
    st = time.time()
    times = 0
    while self.response != '':
        try:
            num_bytes = self.sock.send(self.response)
            l.info('msg wrote %s %d : %r size %r',self.ip,self.port,self.response[:num_bytes],num_bytes)
            self.response = self.response[num_bytes:]                    
        except socket.error,e:
            if e[0] in (errno.EWOULDBLOCK,errno.EAGAIN):
                #here I printed it, but I silent it in normal days
                #print 'would block, again %r',tb.format_exc()
                break
            else:
                l.warning('%r %r socket error %r',self.ip,self.port,tb.format_exc())
            #must break or cause dead loop
            break                    
        except:
            #other exceptions
            l.warning('%r %r msg write error %r',self.ip,self.port,tb.format_exc())
            break    
        times += 1
    et = time.time()

I googled it, and says it caused by temporarily network buffer run out So how can I manually and efficiently detect this error instead it goes to exception phase?

Because it cause to much time to rasie/handle the exception.

© Stack Overflow or respective owner

Related posts about python

Related posts about networking