Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > confusing thread behavior

Reply
Thread Tools

confusing thread behavior

 
 
paul
Guest
Posts: n/a
 
      12-03-2009

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.
 
Reply With Quote
 
 
 
 
Mike Driscoll
Guest
Posts: n/a
 
      12-03-2009
On Dec 3, 3:42*pm, paul <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
paul
Guest
Posts: n/a
 
      12-03-2009
On Dec 3, 2:03*pm, Mike Driscoll <(E-Mail Removed)> wrote:
> On Dec 3, 3:42*pm, paul <(E-Mail Removed)> 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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Confusing difference in raw versus shared_ptr behavior w/ static initializers Rudi Cilibrasi C++ 6 11-06-2011 02:53 AM
confusing input and output stream behavior scigeek C++ 7 09-08-2009 07:51 PM
GridView ... confusing behavior shapper ASP .Net 1 03-09-2007 01:56 PM
Pondering a confusing MaxLength attribute behavior clintonG HTML 4 10-16-2005 11:47 PM
Confusing code behavior in Rails szymon.rozga Ruby 4 06-21-2005 09:34 AM



Advertisments