any way to know when the program is exiting?

M.-A. Lemburg mal at lemburg.com
Thu Jul 29 07:45:01 EDT 1999


Ovidiu Predescu wrote:
> 
> Hi,
> 
> I was wondering if there is any way in a __del__ method to know when it's
> invoked on the way out, as a result of the program going away?

It's a hack, but you can check sys.path: if it's None, then your program
is about to exit. To see why, check the output of 'python -vv -c "None"'.
 
Would probably be a good idea to add some flag to the sys module to be
able to check the current running state of the interpreter:

E.g. sys.state:
	'startup' - interpreter is intializing
	'ready' - interpreter is up and running
	'exiting' - interpreter is exiting

> I have a __del__ method of a class that sends a message to a global variable.
> It happens that on the way out, that value in that variable is destroyed before
> the __del__ methods of my instances are invoked. The result is that I have a
> bogus value that doesn't respond to the usual methods anymore. How can I
> prevent sending messages to that object?
> 
> A possible solution would be to use atexit() but this doesn't work if the user
> program that's using my library replaces it with its own function.

You can use sys.exitfunc() for Python programs.

Hope that helps.
-- 
Marc-Andre Lemburg
______________________________________________________________________
Y2000:                                                   155 days left
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/





More information about the Python-list mailing list