I run this test (following Armin's example):
# in file a.py
class Foo(object):
def __del__(self):
print 'Deleted'
#in file b.py
from a import A
foo=Foo()
#in file c.py
execfile('b.py', {})
execfile('b.py', {})
execfile('b.py', {})
import gc
gc.collect()
running c.py printes
Deleted
Deleted
Deleted
therefore there is NO memory leak.
On Tuesday, February 1, 2011 3:16:12 PM UTC-5, Massimo Di Pierro wrote:
In web2py you cannot store instances of objects into session. WhileArmin makes it tool like this is a web2py problem this is more complexand general than that. In Python if you pickle and object that isinstance of a class defined in /path1/mymodule.py and then youunpickle on a different installation where the class is defined in /path2/mymodule.py you may run into problems. Because web2py does notrequire installation (it is a feature) and has hot plug and play ofapps (another feature) we cannot pickle instances (the price we payfor those features). To me it is worth it.
I agree with Armin that in the case of exec (and web2py uses exec),the self references are created whether you want them or not. Thatmeans one should not define classes in Models and Controllers. I haveissued that warning already to our users and, I have never seen thisbeing a problem in practice.
Nothing in life is perfect and every design decision has a tradeoff.
"Because one of the things we all have to keep in mind: if a Python developer starts his journeys in the twisted world of wrongly executed Python modules they will be very confused when they continue their travels in another Python environment. And having different semantics in different frameworks/modules/libraries is very hurtful for Python as a runtime and language."
I agree. This is a subtle problem that isn't all that obvious to most of us, including me.
I've got classes defined in models (it's sort of the obvious place to define custom validators, for example), and I'd like to understand the implications better than I do.
Anyone? Is using local_import to import a class into a model/controller just as safe as using 'from a import A' (i.e., in terms of memory leaks), or do you have to use the Python import statement directly?