Stopping SocketServer on Python 2.5
metolone+gmane at gmail.com
Thu Mar 12 09:03:06 CET 2009
"Falcolas" <garrickp at gmail.com> wrote in message
news:1b6a95a4-5680-442e-8ad0-47aa9ea08344 at w1g2000prk.googlegroups.com...
>On Mar 11, 1:11 pm, David George <d... at eatmyhat.co.uk> wrote:
>> Again, problem here is the issue of being unable to kill the server
>> while it's waiting on a request. In theory, i could force it to
>> continue by sending some sort of junk data with the method i use to
>> stop the server, but that seems a bit hacky, don't you think?
>I agree, it does.
>I'm in a bit over my head at this point, but does setting
>self.socket.settimeout(0.5) cause the call to get_request (and thus
>self.socket.accept()) to timeout? If so, that may be your ticket,
>since socket.error exceptions are already caught by the TCPServer
Here's the relevant code from Python 2.6's SocketServer.py. It uses
select() to see if a request is waiting to be serviced so handle_request
won't block. If the poll interval expires the while loop will check the
__serving flag. Not ideal, but better than requiring a client to connect
before the flag is checked. The comment lists another idea:
def serve_forever(self, poll_interval=0.5):
"""Handle one request at a time until shutdown.
Polls for shutdown every poll_interval seconds. Ignores
self.timeout. If you need to do periodic tasks, do them in
self.__serving = True
# XXX: Consider using another file descriptor or
# connecting to the socket to wake this up instead of
# polling. Polling reduces our responsiveness to a
# shutdown request and wastes cpu at all other times.
r, w, e = select.select([self], , , poll_interval)
More information about the Python-list