Protocol for thread communication

bockman at virgilio.it bockman at virgilio.it
Wed Mar 5 03:41:56 EST 2008


On 5 Mar, 06:12, Michael Torrie <torr... at gmail.com> 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?
> Currently the main thread is polling some simple status variables.  This
> works, and polling will likely continue to be the simplest and easiest
> way, but simple status variables are very limited.  Are there any
> pythonic patterns people have developed for this.
>
> thanks.
>
> Michael

I've found that Queue.Queue objects are the easiest way to communicate
between threads in Python.
So, I'd suggets that you attach a Queue to each thread: the main
thread will use its queue to receive
the status messages from the other threads; the other threads will use
their queues to receive the retry command (or any other command that
may be needed) from the main thread.

Ciao
------
FB




More information about the Python-list mailing list