Protocol for thread communication

Samuel newsgroups at debain.org
Wed Mar 5 02:15:07 EST 2008


On Tue, 04 Mar 2008 22:12:00 -0700, Michael Torrie wrote:

> Does anyone have any recommended ideas/ways of implementing a proper
> control and status protocol for communicating with threads?  I have a
> program that spawns a few worker threads, and I'd like a good, clean way
> of communicating the status of these threads back to the main thread.
> Each thread (wrapped in a very simple class) has only a few states, and
> progress levels in those states.  And sometimes they can error out,
> although if the main thread knew about it, it could ask the thread to
> retry (start over).  How would any of you do this?  A callback method
> that the thread can call (synchronizing one-way variables isn't a
> problem)?  A queue?  How would the main thread check these things?

I implemented a generic library that could easily support these things:

http://code.google.com/p/exscript/source/browse/trunk/lib/WorkQueue/

Basically, it's an asynchronous queue into which the jobs are added. The 
"MainLoop" class manages jobs (like, making sure that n jobs are running 
at the same time, and providing signals when jobs are done). One way to 
add a status to each job is by adding a status attribute to the Job 
class, in addition, a Job may emit a signal (using a simple signal/event 
mechanism such as this one:

http://code.google.com/p/exscript/source/browse/trunk/lib/Exscript/
Trackable.py

) whenever a status changes, and have the MainLoop class fetch that.

-Samuel



More information about the Python-list mailing list