[Tutor] how do I post event to thread?

Jeff Peery jeffpeery at yahoo.com
Thu Aug 27 17:07:16 CEST 2009


Hi,
thanks for all the good responses. I appreciate your thoughts. I was sightly confused about how these threads work together.
 
I am using python 2.6, no GUI at the moment, so I can't use the wxEvent class or the equivalent. 
 
The program basically pulls data from a serial port and then calculates some stats and writes data to a file. eventually I will put a GUI on it.
 
I think you are correct that since my logic is in its own thread I can put a portion of code that watches the queue.
 
Thanks for the advice!
 
Jeff


--- On Thu, 8/27/09, Kent Johnson <kent37 at tds.net> wrote:


From: Kent Johnson <kent37 at tds.net>
Subject: Re: [Tutor] how do I post event to thread?
To: "Jeff Peery" <jeffpeery at yahoo.com>
Cc: tutor at python.org
Date: Thursday, August 27, 2009, 4:00 AM


On Wed, Aug 26, 2009 at 9:38 PM, Jeff Peery<jeffpeery at yahoo.com> wrote:
> hello,
> I've read a bit about multi thread communication, and found that most people
> use a queue, which makes sense. however in my case I simply have two
> threads, a main thread and one other. the main thread is doing many
> different things and the second thread is receiving numerical data via a
> serial port. when data comes in, I want to post the data to the main thread
> so it can use the it. I don't want to use a queue because I don't want to
> lock up my main thread in a loop that is always looking at the queue waiting
> to get something out of it. I would prefer to do something like post an
> event. The alternative would be to simply have a method in my main thread
> called OnResult(data), and when data comes in to the second thread I could
> call main_thread.OnResult(data). This would solve my problem, but I'm not
> sure if that is an ok thing to do... calling a main thread method from
> within a second thread?

As Dave explained, this will run OnResult() in the data thread.

> is there a way to post an event to the main thread from a second thread?

There is no way I know of to just force a function to run in another
thread. The main thread needs to poll in some way for a notification
from the data thread. If the main thread is running a GUI toolkit,
there is probably a way for the data thread to post an event to the
main thread. Most GUI toolkits also have idle loop callbacks so you
could schedule an idle task to look for data from the data thread.

Does your current main thread have an idle loop where you could poll
the queue for data?

Kent



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090827/e1933585/attachment.htm>


More information about the Tutor mailing list