Are wxPython events posted in order?

167 views
Skip to first unread message

anurag uniyal

unread,
Oct 27, 2009, 3:55:32 AM10/27/09
to wxpytho...@googlegroups.com
If multiple events are posted using wxPostEvent/wxCallAfter from same thread e.g. on button click are they supposed to be processed in same order?

In the below example wx.Callafter is used to post events, which eventually will call the callback functions, will they be called in same order, in which they were passed to wx.CallAfter

def onbutton(self, event):
    wx.CallAfter(func1)
    # some more code
    wx.CallAfter(func2)


rgds
Anurag


Yahoo! India has a new look. Take a sneak peek.

Mike Driscoll

unread,
Oct 27, 2009, 10:00:16 AM10/27/09
to wxpytho...@googlegroups.com
Hi,


On Tue, Oct 27, 2009 at 2:55 AM, anurag uniyal <anurag...@yahoo.com> wrote:
If multiple events are posted using wxPostEvent/wxCallAfter from same thread e.g. on button click are they supposed to be processed in same order?

In the below example wx.Callafter is used to post events, which eventually will call the callback functions, will they be called in same order, in which they were passed to wx.CallAfter

def onbutton(self, event):
    wx.CallAfter(func1)
    # some more code
    wx.CallAfter(func2)


rgds
Anurag


The events should be fired in order since they are added to the stack in that order, but let's find out for sure. I've attached a script that tests that hypothesis.

Every time I press the button, it calls the functions in the right order on my machine. Here's what I'm using:

Windows XP, wxPython 2.8.10.1 (unicode), Python 2.5.

If you also have mouse or timer events bound, they might get backlogged slightly depending on how fast they are firing and the speed of your PC such that you might see some events from them BEFORE you see the functions called although the functions themselves should always be called in the right order. However, I'll leave that test to the reader.

-----------------
Mike Driscoll

Blog:   http://blog.pythonlibrary.org
callAfterTest.py

Mark Guagenti

unread,
Oct 27, 2009, 10:09:26 AM10/27/09
to wxpytho...@googlegroups.com
I get the same results using Ubuntu 8.04 and wxPython 2.8.10.1.

However if you change the code to this:
def onButton(self, event):
wx.CallAfter(self.functionOne)
#wx.CallAfter(self.functionTwo)
wx.CallLater(1, self.functionTwo)

You get different results in Windows than Linux, under Linux
functionTwo fires first!

--Mark

Mike Driscoll

unread,
Oct 27, 2009, 10:26:20 AM10/27/09
to wxPython-users


On Oct 27, 9:09 am, Mark Guagenti <mge...@gentiweb.com> wrote:
> I get the same results using Ubuntu 8.04 and wxPython 2.8.10.1.
>
> However if you change the code to this:
> def onButton(self, event):
>         wx.CallAfter(self.functionOne)
>         #wx.CallAfter(self.functionTwo)
>         wx.CallLater(1, self.functionTwo)
>
> You get different results in Windows than Linux, under Linux
> functionTwo fires first!
>
> --Mark

Well, CallLater allows the programmer to call the function X number of
milliseconds later. So if you set the number of milliseconds high
enough, then you could manipulate which one fires first. My guess is
that CallAfter's wait period is over 1 millisecond on Linux and less
than 1 on Windows.

Robin will probably have a much better (and more technical) answer.

-------------------

Robin Dunn

unread,
Oct 27, 2009, 7:17:40 PM10/27/09
to wxpytho...@googlegroups.com
On 10/27/09 12:55 AM, anurag uniyal wrote:
>
> If multiple events are posted using wxPostEvent/wxCallAfter from same
> thread e.g. on button click are they supposed to be processed in same order?
>
> In the below example wx.Callafter is used to post events, which
> eventually will call the callback functions, will they be called in same
> order, in which they were passed to wx.CallAfter

In general, yes. But there can be situations that can make it appear to
be done out of order. CallAfters and PostEvents are handled by the
app's pending event queue, and this queue is processed at the same time
that the app handles and sends idle events. So for example if something
that you do in one of your CallAfter functions triggers the processing
of idle events, (like calling a yield function or showing a modal
dialog) then the remainder of the queued CallAfters can actually be
executed before the first one is finished.


--
Robin Dunn
Software Craftsman
http://wxPython.org

Robin Dunn

unread,
Oct 27, 2009, 7:17:55 PM10/27/09
to wxpytho...@googlegroups.com


It's probably just a difference of the priority assigned to timer events
on each platform.

anurag...@yahoo.com

unread,
Oct 28, 2009, 5:10:16 AM10/28/09
to wxPython-users
Thanks,

So they are called in order but can interleave if there are any
yields.
Time to grep for Yield :)

Many times i have found very hard to trace bugs using Yield, maybe
they should not be used at all.

Robin Dunn

unread,
Oct 28, 2009, 1:23:10 PM10/28/09
to wxpytho...@googlegroups.com
On 10/28/09 2:10 AM, anurag...@yahoo.com wrote:
>
> Thanks,
>
> So they are called in order but can interleave if there are any
> yields.
> Time to grep for Yield :)
>
> Many times i have found very hard to trace bugs using Yield, maybe
> they should not be used at all.

That is usually the best approach to take, they can be problematic in
some situations.

Reply all
Reply to author
Forward
0 new messages