Python, threading

Scott David Daniels Scott.Daniels at Acm.Org
Thu Dec 11 14:45:25 EST 2008


SMALLp wrote:
> ... I need a tip on how to communicat4 between threads.
Typically inter-thread communication is done via Queue.Queue.
Look up the Queue module in your docs.

a "Simple" example:

     import Queue
     shared_work = Queue.Queue()
     combined_replies = Queue.Queue()
     ... [distributor]
         in loop:
              shared_work.put(something_to_do)
              ...
         for n in range(workers):
             shared_work.put(None) # tell everybody to stop
         for thread in workers:
             thread.join(worker) # wait til they all finish
     combined_replies.put(None) # tell service to stop
     thread.join(writer)    # wait til it is done.

     ... [workers]
              ...
              for work in iter(shared_work.get, None):
                   <do work>
                   combined_replies.put('whatever')
              ...
     ... [writer]
              for results in iter(combined_results.get, None):
                   <use results>

--Scott David Daniels
Scott.Daniels at Acm.Org




More information about the Python-list mailing list