Python select.epoll()。poll(),只轮询eventmask 25

时间:2014-02-05 15:28:15

标签: python epoll

我创建了一个带有RequestHandler的服务器,该服务器继承自使用epoll轮询socketEvents和Qevents的SocketServer.BaseRequestHandler

我最初几次将客户端连接到服务器时,一切都按预期工作,直到某个时候(似乎是随机的)epoll.poll()仅轮询事件[(37,25)],%CPU变为99%。 !但是,客户端不会引发任何套接字错误。

我的handle()方法如下(RequestHandler的一部分)

  def handle(self):
    """"
    This method will process the incoming request while exit is not set
    """
    try:
        while not self.exit.is_set():

            events = self.epoll.poll(1)
            print events
            if self.socketEvent in events:
                self.handle_request()

            if self.QEvent in events:
                self.send_response()

    finally:
        self.finish()
        return

我把print语句放在那里只是为了看看我得到了什么,当一切正常时,我得到[(37,1)](socketEvent)和[(41,1)](QEvent)。发生了什么事?

1 个答案:

答案 0 :(得分:0)

如果有人对此感兴趣,那么我发现finish()中的SocketServer.BaseRequestHandler方法无法处理已关闭的连接。我所做的就是关注(当客户端意外关闭连接时也会这样做):

def handle(self):
""""
This method will process the incoming request while exit is not set
"""
try:
    while not self.exit.is_set():

        events = self.epoll.poll(1)
        print events
        if self.socketEvent in events:
            self.handle_request()

        if self.QEvent in events:
            self.send_response()

        if (self.socketEvent[0],25) in events:
            self.exit.set()

finally:
    self.request.close()
    return