Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Gracefully exiting CLI application
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
David  
View profile  
 More options Jul 27 2009, 4:35 pm
Newsgroups: comp.lang.python
From: David <71da...@libero.it>
Date: Mon, 27 Jul 2009 22:35:01 +0200
Local: Mon, Jul 27 2009 4:35 pm
Subject: Gracefully exiting CLI application
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jan Kaliszewski  
View profile  
 More options Jul 27 2009, 5:35 pm
Newsgroups: comp.lang.python
From: "Jan Kaliszewski" <z...@chopin.edu.pl>
Date: Mon, 27 Jul 2009 23:35:23 +0200
Local: Mon, Jul 27 2009 5:35 pm
Subject: Re: Gracefully exiting CLI application
27-07-2009 o 22:35:01 David <71da...@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>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Finney  
View profile  
 More options Jul 27 2009, 5:40 pm
Newsgroups: comp.lang.python
From: Ben Finney <ben+pyt...@benfinney.id.au>
Date: Tue, 28 Jul 2009 07:40:32 +1000
Local: Mon, Jul 27 2009 5:40 pm
Subject: Re: Gracefully exiting CLI application

David <71da...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jan Kaliszewski  
View profile  
 More options Jul 27 2009, 5:52 pm
Newsgroups: comp.lang.python
From: "Jan Kaliszewski" <z...@chopin.edu.pl>
Date: Mon, 27 Jul 2009 23:52:27 +0200
Local: Mon, Jul 27 2009 5:52 pm
Subject: Re: Gracefully exiting CLI application
27-07-2009 Ben Finney <ben+pyt...@benfinney.id.au> wrote:

> David <71da...@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) <z...@chopin.edu.pl>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nobody  
View profile  
 More options Jul 28 2009, 9:31 am
Newsgroups: comp.lang.python
From: Nobody <nob...@nowhere.com>
Date: Tue, 28 Jul 2009 14:31:56 +0100
Local: Tues, Jul 28 2009 9:31 am
Subject: Re: Gracefully exiting CLI application

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David  
View profile  
 More options Jul 28 2009, 2:44 pm
Newsgroups: comp.lang.python
From: David <71da...@libero.it>
Date: Tue, 28 Jul 2009 20:44:32 +0200
Local: Tues, Jul 28 2009 2:44 pm
Subject: Re: Gracefully exiting CLI application
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »