[2.2.1]How To Gracefully Shutdown an XML-RPC Server
adalke at mindspring.com
Sun Sep 28 03:49:40 CEST 2003
> Thanx ! I *swear* I always spend at least a couple hours or so looking
> for stuf like this on my own !
The Python Cookbook is an impressive collection of tidbits.
> BTW, I haven't tried it, but I bet os._exit will fail just like
Unlikely. sys.exit actually raises a Python exception,
SystemExit. This is caught by the XMLPRC server
and converted into a message for the client.
(And this a reason you shouldn't use "except:" in
your code. Though if you really need it, you should
os._exit uses the C function '_exit' which does an exit
right away, without doing the normal cleanups that
exit(3C) does. It doesn't raise an exception and cannot
be overriden by other Python code. (Unless os._exit
> Besides the reason for a graceful shutdown is more
> than cosmetic, since a forced shutdown (like a Ctrl-C) may occasionally
> leave the sockets allocated (which *has* happened.)
Let me guess, you quit a server program then restarted
it only to get a "cannot rebind to port" error message?
Ctrl-C raises a KeyboardInterrupt exception and its
effect should be the same as SystemExit, except for a
couple very minor issues. It isn't the same as using
What you're seeing is a consequence of the code
not properly handling its network resources. Eg,
it could register an atexit handler which forces
shut all open sockets.
for a description of what's happening at the TCP level.
Even when that happens, it doesn't "leave the socket
[permanently] allocated" in the system level. If you
wait long enough, the system should release the port.
dalke at dalkescientific.com
More information about the Python-list