any way to know when the program is exiting?

Ovidiu Predescu ovidiu at cup.hp.com
Thu Jul 29 13:40:12 EDT 1999


On Thu, 29 Jul 1999 13:45:01 +0200, "M.-A. Lemburg" <mal at lemburg.com> wrote:

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

Thanks, this seems to work. I looked on the source code and it seems that the 
sys
module is treated specially in the cleanup phase.

Checking to see if my global variable is None doesn't work though. I guess this
happens because I use some weak references to avoid cyclic references and my
WeakReference C extension type gets collected before the classes that use it.
The global variable I'm trying to access from the __del__ method gets replaced
by a string object somehow, resulting in an exception being thrown (and ignored
by the interpreter).

> 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 agree, this would be nice.

Thanks a lot for all the responses!

Ovidiu

-- 
Ovidiu Predescu <ovidiu at cup.hp.com>
http://andromeda.cup.hp.com/  (inside HP's firewall only)
http://www.geocities.com/SiliconValley/Monitor/7464/






More information about the Python-list mailing list