How to kill a SocketServer?

Paul Rubin http
Sun May 1 02:58:18 EDT 2005


Let's say you have a SocketServer with the threading mix-in and you
run serve_forever on it.  How can you shut it down, or rather, how can
it even shut itself down?  Even if you use a handle_request loop
instead of serve_forever, it still seems difficult:

     class myserver(ThreadingMixIn, TCPServer): pass
     server = myserver(...)
     server.shutdown = False
     while not server.shutdown:
         server.handle_request()

Some code in the request handler eventually sets server.shutdown to
True (say on receiving a "shutdown" command from the client), but by
then the main thread is already blocked waiting for another
connection.  I ended up having the quit operation actually open a
loopback connection after setting the shutdown flag, just to unblock
the socket listener.  But that seems insane.

Is there a better way?



More information about the Python-list mailing list