Python's collection.deque does not seem to be thread-safe with multiprocessing.Pool
<p>I am working in a small piece of code that is intended to work as follows:</p>

<ul>
<li>An arbitrary number of processes (e.g. 10) is to be run.</li>
<li>Because I have two threads, I want to run them two at a time.</li>
<li>I want them to read a value from a queue. The queue only contains two different values, and I want the two processes running at the same time to read these different values.</li>
</ul>

<p>For instance, consider the next sample code:</p>

<pre><code>import collections
import time

q = collections.deque()
q.append(0)
q.append(1)

def f(i):
    d = q.popleft()
    print d
    time.sleep(1)
    q.append(d)

from multiprocessing import Pool
t = Pool(2)
t.map(f, range(10))
</code></pre>

<p>When running this, I expect the first pair of processes to read 0 and 1 respectively. I don't really mind the order in which they are printed (either 0, 1 or 1, 0). However, the output is as follows:</p>

<pre><code>0
0
1
1
0
0
1
1
0
1
</code></pre>

<p>Why is this happening if deque is thread-safe? Also, the first 8 values are printed 2 at a time every 1 second, but for the last 2 values there is a 1-second pause as well between them. To clarify this, let me draw 1-sec pauses as hyphens:</p>

<pre><code>0
0
-
1
1
-
0
0
-
1
1
-
0
-
1
</code></pre>

<p>Why is this happening?</p>

<p>Thanks a lot.</p>