python threading

Alex Martelli aleax at aleax.it
Tue Apr 16 09:35:00 EDT 2002


Boudewijn Rempt wrote:
        ...
>>     http://starship.python.net/crew/aahz/OSCON2001/index.html
>> 
>> The Queue module might help you with passing values between threads, as
>> it's specifically designed to be thread-safe.
>> 
> 
> That's funny -- only yesterday this cropped up too, and Alex Martelli sent
> me a link to:
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82965, where I
> quickly added a PyQt version of the example code... It uses Queue.
> Actually instantiating GUI objects from different threads might be hard. I
> would place instantiation requests on the Queue, and have the GUI thread
> execute them.

Right!  Queue is THE way to go.  Now, life would be perfect if threads
generally devoted to such tasks as GUI's or networking didn't have to
_periodically poll_ to check if there's something new on the Queue for
them (with a timeout in select, a Tkinter .after callback, a QTimer
instance in Qt, etc etc).  I.e., if Queue had some way to "jig" the
waiting thread out of its waiting state, when new stuff is posted to
the Queue, in such a way that the thread knows it must check its incoming 
Queue.  With select, I guess you could do it with an auxiliary socket
pair (but in Windows...?).  Not sure how to achieve that in the various GUI 
toolkits -- particularly, how to do it without having a call on the GUI 
from a thread that shouldn't be calling the GUI directly.  Polling is
quite a general concept, of course, but unsatisfactory in several
other ways (too rarely -> unresponsive, too often -> wasteful of
resources... what's a poor thread to DO?!-).


Alex




More information about the Python-list mailing list