Resource cleanup
Robin Becker
robin at NOSPAMreportlab.com
Sun Dec 3 07:13:30 EST 2006
I'm thinking of using Tim Peters' excellent approach to resource clean
up see
http://mail.python.org/pipermail/python-dev/2006-April/063425.html
> class _RealTypeResourceCleaner:
> def __init__(self, *resources):
> self.resources = resources
>
> def __del__(self):
> if self.resources is not None:
> for r in self.resources:
> r.close()
> self.resources = None
>
> # and typically no other methods are needed, or desirable, in
> # this helper class
>
> class RealType:
> def __init__(*args):
> ...
> # and then, e.g.,
> self.cleaner = _ResourceCleaner(resource1, resource2)
but am wondering exactly what 'resources' are left available when the
r.close method is called in the __del__ method of RealTypeResourceCleaner.
In particular, can I rely on the module globals of r still being present
if the RealType instance is going away because the main script has
terminated, ie if the r.close method refers to a global function is it
guaranteed to be available when the close is called?
I guess I must be asking if referring to a global in a method is
actually a reference to that global or does the reference only occur
when the code is executed?
I have a vague feeling that I came across problems in the past about the
order in which modules were finalized.
--
Robin Becker
More information about the Python-list
mailing list