Releasing weakrefed objects immediately?

M

Matti Airas

Hi all,

I've been toying with an idea of dependency-based coding, in which you
construct a graph of code dependencies. Say, you have independent
variables 'a' and 'b', and a function f(x,y). Now, you can define that a,b
are dependencies for f(a,b). Whenever a or b are changed, f(a,b) is marked
dirty, and if its value is requested, all the dirty dependencies are
recomputed to evaluate the value of f(a,b). Basically the same idea as in
makefiles.

I also want to have listener objects, so that I could make a GUI text box
depend on f(a,b) so that its value is updated whenever a or b are
modified. So far so good, and all of this I have already implemented. My
problem, however, is that I want to be able to destroy the listener
objects at will, for example when a window is closed. Unfortunately, even
though the only real reference to an object is destroyed, the weak
references seem to keep the listener alive (or the garbage collector won't
kick in for some other reason, even though explicitly called).

Here is the code snippet for my test case:

def testlistener(self):
self.input1 = Dep(value=3,id="input1")
self.input2 = Dep(value=4,id="input2")
self.output1 = Dep((self.input1,self.input2),lambda x,y: x*y,
id="output1")
self.listenerResult = 0
def listenerFunc(self,x):
self.listenerResult = x
self.listener1 = Dep((self.output1,),
lambda x: listenerFunc(self,x),
id="listener1",
isListener=True)
self.assertEqual(self.listenerResult,12)
self.input1.value = 5
self.assertEqual(self.listenerResult,20)
#self.listener1._code = lambda x: x
del self.listener1
gc.collect()
self.input1.value = 4
self.assertEqual(self.listenerResult,20)

The self.listenerResults always is 16 in the final asserEqual, despite
that I have deleted the listener. If I "neuter" the listener by
uncommenting the commented line, I get the right result. So, how could I
make sure that the listener is no longer called when the only strong ref
is removed?

The actual depcode class with the complete testcase and another example
using scipy and matplotlib is available at
http://www.mairas.net/tmp/depcode/ . Note that it is very much code in
progress at the moment.

Thanks for any hints,

Matti Airas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top