[Tutor] Using Queue

Kent Johnson kent37 at tds.net
Wed Apr 23 17:02:33 CEST 2008


On Wed, Apr 23, 2008 at 9:46 AM, bob gailer <bgailer at gmail.com> wrote:

> Evey time someone recommends Queue I think "oh boy this will really help
> me". Then I go to the Library Reference, read the Queue docs and think "oh
> boy who can help me understand this". Even the sample code is confusing.


Can you say what is confusing about it? Do you have a specific use in mind?
Do you understand threading? Queue is used to facilitate communication
between threads, so any Queue example includes multiple threads.

In the simplest case, you have producer and consumer threads, and a single
shared instance of Queue. Each producer thread adds items to the queue using
Queue.put(). Consumer threads remove items with Queue.get(). The benefits:
- get() and put() are thread safe
- get() blocks if the queue is empty - no need for a polling loop
- put() blocks if the queue is full

Additionally if the consumers call Queue.task_done() for each consumed item,
Queue.join() will block until all tasks are complete, then continue.

Kent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080423/69f52f34/attachment-0001.htm>


More information about the Tutor mailing list