How do you register cleanup code to be run after script execution?
Peter Otten
__peter__ at web.de
Tue May 11 02:00:21 EDT 2004
pxlpluker wrote:
> why not use
> try:
> code
> finally:
> cleanup code
Because cleanup code will run immediately after code (regardless of an
exception that might occur in code) whereas atexit.register(cleanupCode)
will trigger the cleanupCode() function when the script terminates. You can
think of atexit as
try:
whole script
finally:
for f in registeredFunctions:
f()
You could implement this manually as you have suggested, but then you'd need
to know what cleanup code is necessary for every module you imported. If
you have modules needing cleanup that aren't always imported, you'd even
have to check for these. Putting atexit.register(cleanupForModule) into
every module needing cleanup doesn't affect its public interface and thus
simplifies client code.
Peter
More information about the Python-list
mailing list