[Python-Dev] atexit missing an unregister method

Nick Jacobson nicksjacobson at hotmail.com
Tue Apr 26 21:57:17 CEST 2005


I was looking at the atexit module the other day; it seems like an elegant 
way to ensure that resources are cleaned up (that the garbage collector 
doesn't take care of).

But while you can mark functions to be called with the 'register' method, 
there's no 'unregister' method to remove them from the stack of functions to 
be called.  Nor is there any way to view this stack and e.g. call 'del' on a 
registered function.

This would be useful in the following scenario, in which x and y are 
resources that need to be cleaned up, even in the event of a program exit:

import atexit

def free_resource(resource):
    ...

atexit.register(free_resource, x)
atexit.register(free_resource, y)
# do operations with x and y, potentially causing the program to exit
...
# if nothing caused the program to unexpectedly quit, close the resources
free_resource(x)
free_resource(y)
#unregister the functions, so that you don't try to free the resources 
twice!
atexit.unregisterall()


Alternatively, it would be great if there were a way to view the stack of 
registered functions, and delete them from there.

--Nick Jacobson

_________________________________________________________________
Don’t just search. Find. Check out the new MSN Search! 
http://search.msn.click-url.com/go/onm00200636ave/direct/01/



More information about the Python-Dev mailing list