Hi Peter,
Peter Otten said:
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 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