Stopping SocketServer on Python 2.5
dave at eatmyhat.co.uk
Wed Mar 11 20:11:36 CET 2009
On 2009-03-11 19:02:26 +0000, Falcolas <garrickp at gmail.com> said:
> On Mar 11, 12:28 pm, David George wrote:
>> On 2009-03-11 04:36:29 +0000, "Mark Tolonen" <metolone+gm... at gmail.com> s
>>> "David George" <d... at eatmyhat.co.uk> wrote in message
>>> news:00150e67$0$27956$c3e8da3 at news.astraweb.com...
>>>> Hi guys,
>>>> I've been developing some code for a university project using Python.
>>>> We've been working on an existing codebase, cleaning it up and removin
>>>> dead wood.
>>>> We decided to make some changes to internal message handling by using
>>>> SocketServer, which worked great when we were using 2.6, as we could
>>>> simply call its shutdown() method when we wanted to stop it from
>>>> 'serving forever'.
>>>> Unfortunately, we've now needed to downgrade to python 2.5 to
>>>> accomodate the libtorrent python bindings we need to use as part of th
>>>> So, my question is, is there any way to stop a SocketServer that's bee
>>>> told to server forever in python 2.5?
>>> Sure, derive a class from one of the classes in SocketServer, and
>>> override the methods that implement the shutdown behavior in 2.6.
>> Based on what you guys have said i've had a look at the code for
>> serve_forever() in both 2.5 and 2.6, and tried to create my own derived
>> class in this manner:
>> class MBThreadingTCPServer(SocketServer.ThreadingTCPServer):
>> def __init__(self, address_tuple, handler):
>> SocketServer.ThreadingTCPServer.__init__(self, address_tu
> ple, handler)
>> self.__serving = True
>> def serve_forever(self):
>> while self.__serving:
>> def shutdown(self):
>> self.__serving = False
>> Don't worry about the MB thing, it's just related to the name of our proj
>> I don't think i've done this right, but i've tried to implement the
>> serve_forever() functionality in my derived class, and also add the
>> shutdown() method so i can stop it.
>> Does this appear to be the right way to do things?
> More or less what I would do, though you should be able to call
> self.handle_request. It's worth noting that handle_request generally
> calls a blocking socket method, which means your self.__serving check
> only happens the next time it handles a request.
Yes, i've just noticed that this is the problem ... i've updated the
code to work like this:
self.serving = True
self.has_shutdown = False
self.has_shutdown = True
self.serving = False
Simply calling the base class constructor when i build it (i believe
this is inferred).
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?
More information about the Python-list