Register function to call at shutdown
Is it possible to register a function to be called at finalization time? By finalization time I'm referring to the point at which Python is still operational, but is shutting down, for example the time when the modules are getting cleaned up. Functions registered with Py_AtExit get called too late, when it's no longer allowed to invoke any Python API function.
I currently simulate this using an object whose __del__ invokes my function, and sticking that object in a private module. During module cleanup Python unhooks the object from the module, firing its __del__ which calls my function. But I consider it a hack, and a fragile one at that -- if anyone deletes the object from the module, the function will be called too early. I'd prefer a cleaner way to deal with this if possible.
On 2008-01-17 13:49, Hrvoje Niksic wrote:
Is it possible to register a function to be called at finalization time? By finalization time I'm referring to the point at which Python is still operational, but is shutting down, for example the time when the modules are getting cleaned up. Functions registered with Py_AtExit get called too late, when it's no longer allowed to invoke any Python API function.
I currently simulate this using an object whose __del__ invokes my function, and sticking that object in a private module. During module cleanup Python unhooks the object from the module, firing its __del__ which calls my function. But I consider it a hack, and a fragile one at that -- if anyone deletes the object from the module, the function will be called too early. I'd prefer a cleaner way to deal with this if possible.
Why don't you use the standard atexit module for this ?
This still only works from Python, but you can add your function in a much more reliable way.
-- Marc-Andre Lemburg eGenix.com
Professional Python Services directly from the Source (#1, Jan 18 2008)
Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611
"M.-A. Lemburg" mal@egenix.com writes:
Why don't you use the standard atexit module for this ?
That's exactly what I was looking for, thanks!
(Don't know how I missed it.)
This still only works from Python, but you can add your function in a much more reliable way.
That's not a problem because I can still access it from Python/C using PyModule_Import and friends.
participants (2)
-
Hrvoje Niksic
-
M.-A. Lemburg