Gracefully exiting CLI application
Jan Kaliszewski
zuo at chopin.edu.pl
Mon Jul 27 17:52:27 EDT 2009
27-07-2009 Ben Finney <ben+python at benfinney.id.au> wrote:
> David <71david at 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).
*j
--
Jan Kaliszewski (zuo) <zuo at chopin.edu.pl>
More information about the Python-list
mailing list