any way to know when the program is exiting?

Markus Stenberg mstenber at cc.Helsinki.FI
Thu Jul 29 03:26:59 EDT 1999


Ovidiu Predescu <ovidiu at cup.hp.com> writes:
> 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?

AFAIK, not..

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

Keep reference to the global variable in the classes with __del__.

Example follows: (How I kill child processes, and ensure the os module is
garbage collected _after_ the DelWrapper instance is __del__'d)

class DelWrapper:
    """
quick-n-dirty class to wrap __del__
"""
    def __init__(self, f):
        self.__del__ = f

def foo():
	... # proprietary, super-secret code snipped
	global __tmp
	__tmp = DelWrapper(lambda k=os.kill,pid=pid:apply(k, (pid, 15)))

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


> Any ideas?

Occassionally, some ;)

-Markus

-- 
   "I am Grey. I stand between the candle and the star.
  We are Grey. We stand between the darkness and the light."
  - Delenn / Babylon 5 [ #1 scifi show ATM, IMO ]




More information about the Python-list mailing list