Hi,
Dear all,
I am very curious about how the wx.CallAfter is implemented,
especially comparing the approach of using the Queue to make threads
communicate.
From various posts on the internet as well as on Robin's book, there
is only a short and vague description, like "the function object
passed to wx.CallAfter, which is placed in the secondary thread, will
be called or executed by the main GUI thread, after the current
processing completes."
My question is "what is the relation with multi-thread programming and
event-processing? How wx.CallAfter handles this?", I think it would be
much better to read the code to understand it. I tried to find the
wx.CallAfter implementation from wxPython source code distribution,
but unfortunately did not find it.
Can anyone point out where this part is?
All it does is takes a function as an argument, sets it as a value on
an event, posts the event to the App object. The event is put in the
event queue and when the main loop iterates to handle that event it
processes it and calls the method that was attached to the event
object.
It's in _core.py of your installation (directory search )
def CallAfter(callable, *args, **kw):
"""
Call the specified function after the current and pending event
handlers have been completed. This is also good for making GUI
method calls from non-GUI threads. Any extra positional or
keyword args are passed on to the callable when it is called.
:see: `wx.CallLater`
"""
app = wx.GetApp()
assert app is not None, 'No wx.App created yet'
if not hasattr(app, "_CallAfterId"):
app._CallAfterId = wx.NewEventType()
app.Connect(-1, -1, app._CallAfterId,
lambda event: event.callable(*event.args, **event.kw) )
evt = wx.PyEvent()
evt.SetEventType(app._CallAfterId)
evt.callable = callable
evt.args = args
evt.kw = kw
wx.PostEvent(app, evt)
Cody
···
On Wed, Jan 26, 2011 at 7:43 AM, pploo <beilushao@gmail.com> wrote: