Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Gracefully exiting CLI application

8 views
Skip to first unread message

David

unread,
Jul 27, 2009, 4:35:01 PM7/27/09
to
Greetings,

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.

Jan Kaliszewski

unread,
Jul 27, 2009, 5:35:23 PM7/27/09
to David, pytho...@python.org
27-07-2009 o 22:35:01 David <71d...@libero.it> wrote:

> 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>

Ben Finney

unread,
Jul 27, 2009, 5:40:32 PM7/27/09
to
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.

--
\ “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

Jan Kaliszewski

unread,
Jul 27, 2009, 5:52:27 PM7/27/09
to Ben Finney, pytho...@python.org
27-07-2009 Ben Finney <ben+p...@benfinney.id.au> wrote:

> 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).

Nobody

unread,
Jul 28, 2009, 9:31:56 AM7/28/09
to
On Mon, 27 Jul 2009 22:35:01 +0200, David wrote:

> 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.

David

unread,
Jul 28, 2009, 2:44:32 PM7/28/09
to
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.

0 new messages