How to kill a SocketServer?
Andrew Dalke
dalke at dalkescientific.com
Sun May 1 21:58:03 EDT 2005
After I gave a reference to CherryPy's threaded SocketServer-based
http server code Paul Rubin followed up:
> Well, ok, so the worker threads stop. How do you get the listener
> thread to stop, since it's blocked waiting for a new connection to arrive?
I don't know the code well enough. You might want to look
through it yourself. I think it works by implementing a
new 'server_activate()' to implement a timeout
class CherryHTTPServer(BaseHTTPServer.HTTPServer):
def server_activate(self):
"""Override server_activate to set timeout on our listener
socket"""
self.socket.settimeout(1)
BaseHTTPServer.HTTPServer.server_activate(self)
followed by a check for the timeeout
def handle_request(self):
"""Override handle_request to trap timeout exception."""
try:
BaseHTTPServer.HTTPServer.handle_request(self)
except socket.timeout:
# The only reason for the timeout is so we can notice keyboard
# interrupts on Win32, which don't interrupt accept() by default
return 1
except KeyboardInterrupt:
_cpLogMessage("<Ctrl-C> hit: shutting down", "HTTP")
self.shutdown()
with the redefined serve_forever() done as
def serve_forever(self):
"""Override serve_forever to handle shutdown."""
self.__running = 1
while self.__running:
self.handle_request()
The __running attribute is set in the shutdown() method
def shutdown(self):
self.__running = 0
It looks like some special attention is needed when the socket
is in timeout mode
def get_request(self):
# With Python 2.3 it seems that an accept socket in timeout (nonblocking
) mode
# results in request sockets that are also set in nonblocking mode. Sin
ce that doesn't play
# well with makefile() (where wfile and rfile are set in SocketServer.p
y) we explicitly set
# the request socket to blocking
request, client_address = self.socket.accept()
request.setblocking(1)
return request, client_address
Hmmm, and I've just copied and pasted the entire implementation of
class CherryHTTPServer(BaseHTTPServer.HTTPServer):
given in my CherryPy-2.0.0/cherrypy/_cphttpserver.py
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list