weak reference to bound method

Ole Streicher ole-usenet-spam at gmx.net
Fri Oct 2 08:50:28 EDT 2009


Hi Peter,

Peter Otten <__peter__ at web.de> writes:
> Ole Streicher wrote:
>> Peter Otten <__peter__ at web.de> writes:
>>>> What I want is to have a universal class that "always" works: with
>>>> unbound functions, with bound function, with lambda expressions, with
>>>> locally defined functions,
>>> That's left as an exercise to the reader ;)
>> Do you have the feeling that there exists any reader that is able to
>> solve this exercise? :-)
> I was thinking of you.

I could imagine that. However, I am just a beginner in Python and I dont
know which types "callables" there exist in python and which "smart"
ideas (like re-creating them at every call) additionally occur when I
implement such a beast. For example, for locally defined functions, I
have still no idea at all on how to keep them away from the garbage
collector.

>> I am a bit surprised that already such a simple problem is virtually
>> unsolvable in python. Do you think that my concept of having a DoAsync
>> class is wrong?
> I don't understand the example you give in the other post. 

Hmm. I am programming a GUI client application. The client will receive
some input data (via network, and via user input) and shall be updated
after these data.

Unfortunately, the input data (and ofcourse the user input) do not come
regularly; there may times when the data come too fast to process all
of them.

Imagine, for example, that I want to provide a 2d-gaussian fit to some
region of an image and display the result in a separate window, and
updated this whenever the mouse is moved.

The fit takes (let's say) some seconds, so I cannot just call the fit
routine within the mouse move event (this would block other gui
operations, and f.e. the display of the mouse coordinates). So I need
just to trigger the fit routine on mouse movement, and to check
afterwards whether the mouse position is still current.

This is the reason for the DoAsync class: when it is called, it shall
trigger the function that was given in the constructor, t.m.

class MyClass:
    def __init__(self):
        self.update_fit = DoAsync(update_the_fit)

    def mouse_move(self, event):
        self.set_coordinates(event.x, event.y)
        self.update_fit() # trigger the update_the_fit() call
    ...

Thus, the mouse_move() method is fast, even if the update_the_fit()
method takes some time to process.

I want to implement it now that DoAsync will be automatically garbage
collected whenever the MyClass object is deleted. Since DoAsync starts
its own thread (which only shall finish when the MyClass object is
deleted), a reference to MyClass (or one of its functions) will keep the
MyClass object from garbage collection.

> If you are trying to use reference counting as a means of inter-thread 
> communication, then yes, I think that's a bad idea.

No; my problem is:

- a thread started in DoAsync will keep the DoAsync object from
  garbage collection
- a reference to a MyClass realted object (the bound method) in DoAsync 
  will thus also prevent the MyClass object from garbage collection
- Even if I dont use the MyClass object anymore, and nobody else uses 
  the DoAsync object, both stand in memory forever, and the thread also 
  never finishes.

Did you get the problem?

Best regards

Ole



More information about the Python-list mailing list