Gracefully exiting CLI application

David 71david at libero.it
Tue Jul 28 14:44:32 EDT 2009


Il Tue, 28 Jul 2009 14:31:56 +0100, Nobody ha scritto:

> Killed by what means?
> 
> Ctrl-C sends SIGINT which is converted to a KeyboardInterrupt exception.
> This can be caught, or if it's allowed to terminate the process, any exit
> handlers registered via atexit.register() will be used.
> 
> For other signals, you can install a handler with signal.signal(). This
> can call sys.exit() or raise an exception (e.g. KeyboardInterrupt).
> 
> OTOH, if the process is terminated by SIGKILL, there's nothing you can do
> about it. And although it's possible to trap SIGSEGV, you shouldn't assume
> that the Python interpreter is still functional at this point.

I'm really sorry guys, I forgot to mention that the platform is win32, where
there is minimal support to signals.

Anyway I've found a solution with win32api.SetConsoleCtrlHandler that
installs a event handler which traps all windows events, CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT, and CTRL_SHUTDOWN_EVENT included.

David.



More information about the Python-list mailing list