confusing thread behavior

Discussion in 'Python' started by paul, Dec 3, 2009.

  1. paul

    paul Guest

    I have been experiencing strange thread behavior when I pass a message
    received via a Queue to a wx.PostEvent method (from wxPython). The
    relevant code in the thread is:

    def run(self):
    while self.is_running:
    task = self.queue.get()
    wx.PostEvent(self.app.handle_task, task)

    self.queue is a Queue.Queue instance and self.app is a wx.Window
    instance

    I have a case where two items are placed in the queue one after the
    other, and it appears that only the first item is passed to
    wx.PostEvent. If I place a time.sleep(0.1) call anywhere within the
    while loop, both items get passed to wx.PostEvent. It works if I put
    time.sleep before the self.queue.get(), in between get() and PostEvent
    () or after PostEvent(). So it seems like a short delay is enough to
    get two items handled, although it doesn't seem to matter where I
    place the delay within the while loop. Does someone know what might
    explain this behavior? Thanks.
     
    paul, Dec 3, 2009
    #1
    1. Advertising

  2. On Dec 3, 3:42 pm, paul <> wrote:
    > I have been experiencing strange thread behavior when I pass a message
    > received via a Queue to a wx.PostEvent method (from wxPython). The
    > relevant code in the thread is:
    >
    > def run(self):
    >     while self.is_running:
    >         task = self.queue.get()
    >         wx.PostEvent(self.app.handle_task, task)
    >
    > self.queue is a Queue.Queue instance and self.app is a wx.Window
    > instance
    >
    > I have a case where two items are placed in the queue one after the
    > other, and it appears that only the first item is passed to
    > wx.PostEvent.  If I place a time.sleep(0.1) call anywhere within the
    > while loop, both items get passed to wx.PostEvent.  It works if I put
    > time.sleep before the self.queue.get(), in between get() and PostEvent
    > () or after PostEvent().  So it seems like a short delay is enough to
    > get two items handled, although it doesn't seem to matter where I
    > place the delay within the while loop.  Does someone know what might
    > explain this behavior?  Thanks.


    [Note: I cross-posted this to wxPython where Paul had also cross-
    posted]

    Not sure if this will help or not, but see the following article on
    the wxPython wiki:

    http://wiki.wxpython.org/LongRunningTasks

    There's a section called "More Tips" where Queues are discussed. Robin
    Dunn (creator of wxPython) seems to recommend using wx.CallAfter there
    rather than PostEvent.

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

    Blog: http://blog.pythonlibrary.org
     
    Mike Driscoll, Dec 3, 2009
    #2
    1. Advertising

  3. paul

    paul Guest

    On Dec 3, 2:03 pm, Mike Driscoll <> wrote:
    > On Dec 3, 3:42 pm, paul <> wrote:
    >
    >
    >
    >
    >
    > > I have been experiencing strange thread behavior when I pass a message
    > > received via a Queue to a wx.PostEvent method (from wxPython). The
    > > relevant code in the thread is:

    >
    > > def run(self):
    > >     while self.is_running:
    > >         task = self.queue.get()
    > >         wx.PostEvent(self.app.handle_task, task)

    >
    > > self.queue is a Queue.Queue instance and self.app is a wx.Window
    > > instance

    >
    > > I have a case where two items are placed in the queue one after the
    > > other, and it appears that only the first item is passed to
    > > wx.PostEvent.  If I place a time.sleep(0.1) call anywhere within the
    > > while loop, both items get passed to wx.PostEvent.  It works if I put
    > > time.sleep before the self.queue.get(), in between get() and PostEvent
    > > () or after PostEvent().  So it seems like a short delay is enough to
    > > get two items handled, although it doesn't seem to matter where I
    > > place the delay within the while loop.  Does someone know what might
    > > explain this behavior?  Thanks.

    >
    > [Note: I cross-posted this to wxPython where Paul had also cross-
    > posted]
    >
    > Not sure if this will help or not, but see the following article on
    > the wxPython wiki:
    >
    > http://wiki.wxpython.org/LongRunningTasks
    >
    > There's a section called "More Tips" where Queues are discussed. Robin
    > Dunn (creator of wxPython) seems to recommend using wx.CallAfter there
    > rather than PostEvent.
    >
    > -------------------
    > Mike Driscoll
    >
    > Blog:  http://blog.pythonlibrary.org



    I apologize -- I meant to type wx.CallAfter instead of wx.PostEvent in
    my posting here (I've actually tried both approaches and did not see
    any difference...). I've looked over the LongRunningTasks wiki. I'll
    look at it again!
     
    paul, Dec 3, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. clintonG
    Replies:
    4
    Views:
    607
    clintonG
    Oct 17, 2005
  2. shapper

    GridView ... confusing behavior

    shapper, Mar 9, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    281
    Greg9Strat
    Mar 9, 2007
  3. scigeek
    Replies:
    7
    Views:
    368
    scigeek
    Sep 8, 2009
  4. Rudi Cilibrasi
    Replies:
    6
    Views:
    861
    Rudi Cilibrasi
    Nov 6, 2011
  5. szymon.rozga

    Confusing code behavior in Rails

    szymon.rozga, Jun 21, 2005, in forum: Ruby
    Replies:
    4
    Views:
    122
    Chris McGrath
    Jun 21, 2005
Loading...

Share This Page