开发者

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

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 cod开发者_StackOverflowe 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.


Use select to see if a socket is ready for reading or writing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜