在Python epoll中我可以避免errno.EWOULDBLOCK、errno.EAGAIN吗?

发布于 2024-11-16 11:58:05 字数 1486 浏览 1 评论 0原文

我用python写了一个epoll包装器,它工作得很好,但最近我发现对于大包发送来说性能不太理想。我查看代码,发现实际上有很多错误

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

,并且之前按照文档所述将其静音,所以我的发送功能是这样完成的:

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

維他命╮ 2024-11-23 11:58:05

使用 select 查看套接字是否已准备好读取或写作。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文