Simple question about Queue.Queue and threads

Frank Millman frank at chagford.com
Tue Feb 9 01:45:03 EST 2010


On Feb 8, 4:51 pm, Steven <Steven.Rumbal... at fiserv.com> wrote:
>
> Queue objects have support for this signaling baked in with
> q.task_done and q.join.
>
> After the server process has put all tasks into the queue, it can join
> the queue itself, not the worker threads.
>
> q.join()
>
> This will block until all tasks have been gotten AND completed.  The
> worker threads would simply do this:
> task_data = q.get()
> do_task(task_data)
> q.task_done()
>
> Using pairs of get and task_done you no longer need to send a signal.
> Just exit the server process and the worker threads will die (assuming
> of course, you set .setDaemon(True) before starting each worker
> thread).
>

Thanks, Steven.

This works perfectly in my scenario, and tidies up the code a bit.

Minor point - according to the 2.6 docs, .setDaemon(True) is the old API - 
the current way of specifying this is .daemon = True.

Thanks for the tip.

Frank





More information about the Python-list mailing list