[Tutor] Cleanly stopping a Python server?

Blake.Garretson at dana.com Blake.Garretson at dana.com
Mon Sep 15 09:08:30 EDT 2003


"R. Alan Monroe" <amonroe at columbus.rr.com> wrote:
>Is there a nice way of killing a server that's been launched with
>serve_forever()? (I have SimpleXMLRPCServer in mind, but it would
>apply to this too).

I never use serve_forever.  Instead, try serving one request in a loop
until you tell the server otherwise.

Here's a template:

###
quit = 0

class MyDaemon:
    def kill_server(self):
        global quit
        quit = 1
        # perform clean up functions, etc.
        return 1
    #... other functions, etc.

server = SimpleXMLRPCServer.SimpleXMLRPCServer((slave_host,
slave_port),logRequests=0 )
server.register_instance(MyDaemon())

while not quit:
    server.handle_request()
###

Of course, this requires an external program to bring it down, but that is
simple enough:

###
# kill server
s = xmlrcplib.Server(server_string)
s.kill_server()
###

-
Blake Garretson





More information about the Tutor mailing list