In the example of a asynchronous (threading) SocketServer http://docs.python.org/2/library/socketserver.html a server thread (called server_thread) is started, to start new threads for each request. Due to some problems catching KeyboardInterrupts, I started looking for similar code and found that there's no apparent difference when NOT using a server thread, but ctrl-c actually works.
Even though my code works I'd very much like to know
1) Why does not a simple 'try' to catch KeyboardInterrupt work, when using the server_thread?
2) What good does the server_thread from the example serve - as opposed to my somewhat simpler example?
From the python SocketServer example, catching keyboardinterrupt in try does not work:
if __name__ == "__main__":
server = ThreadedTCPServer(serverAddr, SomeCode)
<snip>
# Start a thread with the server -- that thread will then start one
# more thread for each request
server_thread = threading.Thread(target=server.serve_forever)
server_thread.start()
My simpler example, ctrl-c works.
if __name__ == "__main__":
server = ThreadedTCPServer(serverAddr, SomeCode)
try:
server.serve_forever()
print "ctrl-c to exit"
except KeyboardInterrupt:
print "interrupt received, exiting"
server.shutdown()