Question

I've created a server with a RequestHandler that inherits from the SocketServer.BaseRequestHandler that polls socketEvents and Qevents using epoll.

For the first few times I connect the client to the server everything works as expected, until at some point (seems random) epoll.poll() only polls events [(37,25)], and the %CPU becomes 99%.! However the client doesn't raise any socket error.

My handle() method is as follows (part of the 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

I put the print statement there just to see what I was getting and when everything is working I get [(37,1)] (socketEvent) and [(41,1)](QEvent). What is going on?

Was it helpful?

Solution

If anyone is interested, then I found out that the finish() method in the SocketServer.BaseRequestHandler does nothing to handle closed connections. What I did was following (this also takes care of when the client closes the connection unexpectedly):

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top