On Wed, Apr 23, 2008 at 1:17 PM, bob gailer &lt;<a href="mailto:bgailer@gmail.com">bgailer@gmail.com</a>&gt; wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
&quot;The Queue module implements a multi-producer, multi-consumer FIFO queue.<br>
<br>
I understand producer, comsumer, FIFO.<br>
<br>
I don&#39;t understand multi-</blockquote><div><br>More than one producer and/or consumer<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
<br>
&quot;It is especially useful in threads programming when information must be exchanged safely between multiple threads. &quot;<br>
<br>
I understand threads. I&#39;ve written some (to me fairly sophisticated) programs using Threading and conditions.<br>
<br>
I understand that threads might want to exchange information.<br>
<br>
I guess that queue supports the exchange by receiving and releasing items. Is that true?</blockquote><div><br>Yes. Producers put things in the queue, consumers pull them out. Basically a FIFO where the two ends are used by different threads.<br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
I don&#39;t know what &quot;safely&quot; means.<br>
<br>
&quot;The Queue class in this module implements all the required locking semantics.&quot; I have no idea what that means nor does any of the ensuing documentation explain.</blockquote><div class="Ih2E3d"><br>Ok, do you understand thread safety, or conversely, what kind of problems can happen when two threads share data without any kind of locking? <br>
<br>For example, suppose you want a queue with a limited number of items allowed in it, and producers should block if the queue is full. You need an atomic &quot;check for full and push&quot; operation; otherwise you can have race conditions between two threads. If the queue has one empty slot, you have to protect against<br>
- thread A checks for queue full - not full<br>- thread B checks for queue full - not full<br>- thread A pushes - queue is now full<br>- thread B pushes - overflow<br><br>queue.Queue uses locks to prevent this and other problems.<br>

<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Do you have a specific use in mind?<br>
</blockquote>
<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I have an application that uses Threading. It is not a producer, consumer application, just a bunch of threads that are started at the same time. And they do not communicate with each other, just with the main thread. But that seems to be true of Queue also.</blockquote>
<div><br>Possibly you could use a Queue here. The main thread would be the only consumer; the rest of the threads would be producers.<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d"><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Queue is used to facilitate communication between threads, so any Queue example includes multiple threads.<br>
<br>
<br>
</blockquote></div>
Regarding the sample code:<br>
I must add (by trial and error) at least these 3 lines to the main program:<br>
<br>
from threading import *<br>
from Queue import Queue<br>
num_worker_threads = 3<br>
<br>
Then it bombs:<br>
File &quot;J:\python\queue.py&quot;, line 17, in &lt;module&gt;<br>
 &nbsp; for item in source():<br>
NameError: name &#39;source&#39; is not defined<br>
<br>
No idea what source is supposed to be. A callable object? And where in this example are the producer threads?</blockquote><div><br>Yes, the example is really just a sketch. source is a callable object that returns an iterable object whose items represent some kind of unit of work.<br>
<br>The threads created by calling Thread(target=worker) are the consumer threads. The main thread is the producer - it&nbsp; puts items into the queue in the for loop.<br><br>Kent<br></div></div><br>