how wx.CallAfter is implemented

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?

Thanks a lot!

I understand that wx.Yield uses a nested event loop, wx.CallAfter maybe different though.

···


Hi, I will kill all ads in google gmail.
They will all be dead and gone for all my emails to you. HA HA bye bye ads I just massacred you!!!

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 :wink: )

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:

Can anyone point out where this part is?

CallAfter is defined in wx/_core.py in site-packages/wx-2*
From what I understand, CallAfter will call it's first argument after
the event currently being processed has been handled.

My question is "what is the relation with multi-thread programming and event-processing? How wx.CallAfter handles this?"

In wx only the main GUI thread can edit widgets safely. If you are
using multiple threads in your program (e.g. to perform a CPU
intensive task without blocking the GUI) and want them to update the
GUI when it is complete, then you need to raise an Event from the
external thread and catch that event in your GUI.

CallAfter isn't required in this example. I usually use CallAfter when
I want to perform an action in an event handler, but that action
depends on the event having been handled (which it isn't in my event
handler). There are probably other subtle uses that I'm not aware of.

Craig

···

On Jan 26, 1:43 pm, pploo <beilus...@gmail.com> wrote: