faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 2 people (50%) answered Yes
Recently 0 of 1 people (0%) answered Yes

Entry

Any way to know when the program is exiting?

Jul 5th, 2000 10:01
Nathan Wallace, Hans Nowak, Snippet 197, Markus Stenberg


"""
Packages: operating_systems.unix;miscellaneous.introspection
"""
"""
> 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)))