Resource cleanup

Diez B. Roggisch deets at nospam.web.de
Sun Dec 3 08:52:38 EST 2006


> 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.

I'm a bit on unsure ground here - so take it with a grain of salt.

It is for sure that only executing code will refer to a global - the 
mere mention of anything can't possibly create a reference (in python at 
least) - consider this simple example:

import random

def foo():
     print schroedingers_cat

if random.random() > .5:
     schroedingers_cat = "I'm alive!"

foo()


So I presume it can very well happen that you will lose a module when 
trying to finalize.

So most probably it is the cleverest solution to make the cleaner as 
self-contained as possible, by storing explicit references to things you 
might need in the instance itself. But I'm not sure if the transitivity 
of dependencies might not kick your ass somewhere anyhow.

All in all an interesting topic - I'd be looking forward to more 
insights, and very much liked the link you gave us.


Diez



More information about the Python-list mailing list