Releasing weakrefed objects immediately?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matti Airas

    #1

    Releasing weakrefed objects immediately?

    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(se lf):
    self.input1 = Dep(value=3,id= "input1")
    self.input2 = Dep(value=4,id= "input2")
    self.output1 = Dep((self.input 1,self.input2), lambda x,y: x*y,
    id="output1")
    self.listenerRe sult = 0
    def listenerFunc(se lf,x):
    self.listenerRe sult = x
    self.listener1 = Dep((self.outpu t1,),
    lambda x: listenerFunc(se lf,x),
    id="listener1" ,
    isListener=True )
    self.assertEqua l(self.listener Result,12)
    self.input1.val ue = 5
    self.assertEqua l(self.listener Result,20)
    #self.listener1 ._code = lambda x: x
    del self.listener1
    gc.collect()
    self.input1.val ue = 4
    self.assertEqua l(self.listener Result,20)

    The self.listenerRe sults 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

Working...