I am writing a command line application, and I need to perform some cleaning
on exit even if the process is killed.
How can I do that with python?
Thank you.
David.
> I am writing a command line application, and I need to perform some
> cleaning
> on exit even if the process is killed.
> How can I do that with python?
See: http://docs.python.org/library/signal.html
Cheers,
*j
--
Jan Kaliszewski (zuo) <z...@chopin.edu.pl>
> I am writing a command line application, and I need to perform some
> cleaning on exit even if the process is killed. How can I do that with
> python?
Write an “exit handler” function, then use ‘atexit.register’
<URL:http://docs.python.org/library/atexit> to register yours for
processing whenever the program exits.
--
\ “Pinky, are you pondering what I'm pondering?” “Wuh, I think |
`\ so, Brain, but will they let the Cranberry Duchess stay in the |
_o__) Lincoln Bedroom?” —_Pinky and The Brain_ |
Ben Finney
> David <71d...@libero.it> writes:
>
>> I am writing a command line application, and I need to perform some
>> cleaning on exit even if the process is killed. How can I do that with
>> python?
>
> Write an “exit handler” function, then use ‘atexit.register’
> <URL:http://docs.python.org/library/atexit> to register yours for
> processing whenever the program exits.
Unfortunately neither sys.exitfunc nor function registered with
atexit.register() are called when process is killed with signal.
As I wrote, you must use signals. Though sometimes it's a good idea
to combine these two techniques (i.e. signal handlers call sys.exit(),
then sys.exitfunc/or function registered with atexit does the actual
cleaning actions).
> I am writing a command line application, and I need to perform some
> cleaning on exit even if the process is killed. How can I do that with
> python?
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.
> 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.