Problem with signals & exceptions...

Skip Montanaro skip at mojam.com
Wed Jul 28 17:49:06 EDT 1999


    Eric> I changed my code to set a global variable rather than throw an
    Eric> exception and that works the way you'd expect. A socket.error
    Eric> exception is thrown, and I catch it, check the global variable, if
    Eric> the global variable is set I print "got a timeout!" else re-throw
    Eric> the exception.

I think this is fairly standard practice in Python.  You can avoid the
global variable using a class like:

    class Server(SocketServerTCPServer):
	def __init__(self):
	    self.serving = 1

	def exit(self):
	    do_cleanup_stuff()
	    self.serving = 0

	def server_forever(self):
	    while self.serving:
		self.handle_request()

    server = Server(server_address, request_handler_class)
    signal.signal(signal.SIGHUP, server.exit)
    signal.signal(signal.SIGQUIT, server.exit)
    signal.signal(signal.SIGTERM, server.exit)
    server.serve_forever()

Presumably the Server class is actually a bit more substantial than this,
but this serves to demonstrate hiding the global variable in a bit of state
inside a probably already existing server object.

Skip Montanaro	| http://www.mojam.com/
skip at mojam.com  | http://www.musi-cal.com/~skip/
847-475-3758







More information about the Python-list mailing list