[Tutor] how do I post event to thread?

Dave Angel davea at ieee.org
Thu Aug 27 05:16:35 CEST 2009

Jeff Peery 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?
> is there a way to post an event to the main thread from a second thread?
> Is it ok to pass the main thread (self) into the second thread and have the second thread
> call a main_thread.OnResult(data) method to pass the data into the main thread? I'm not sure how the main thread handles this, my guess is that it can only do one thing at a time so it might be exactly that same as 'posting an event'.
> any thoughts would be much appreciated. thanks!
> Jeff
You have some confusion here.  A function doesn't belong to a thread, 
it's just code.  When that code is called from the main thread, it's a 
function in the main thread.  When the same function is called from a 
second thread, it's a function in that one.  The trick to communicating 
between threads is *not* calling, it's a queue.  You can use standard 
ones, or you can write your own.  And you can call it lots of things.  
But basically the main thread will need to poll the "queue" somehow, to 
decide whether there's anything there to do.

What is your main thread doing?  Is yours a GUI program?  If so, the 
main thread already has a polling loop, and you can just post an event 
on the event queue.  Something like "callafter" or whatever it's called 
in your particular GUI.

A queue of length 1 might be just a flag that's set by the secondary 
thread whenever there's data available.  And it gets cleared by the main 
thread whenever it's noticed and acted upon.

When the OS creates a thread, it basically creates a second stack frame, 
and creates another instance of the static space called "thread local 
storage (TLS)."  Python presumably puts the thread object into the 
thread local storage.  And the OS switches threads by restoring the 
stack pointer to point to a different stack frame, as well as the 
threadpointer to point to whichever TLS corresponds to it.  Most things 
are totally shared between the threads.  There are various OS things 
that affect the scheduling, so for example if you make a blocking call 
to an OS function, a thread switch will let other threads run in the 
meantime.  Presumably an example of that is the call to read the serial 
port.  Another way a thread can voluntarily give up control is with a 
sleep() call.  Hopefully a GUI mainloop does one of those whenever there 
are no events waiting.

There's more subtlety to threads.  But this should get you started, and 
help point the way to questions more specific to your program design.  
Tell us python version, OS, and what GUI library you may be using.


More information about the Tutor mailing list