I'm writing an object which draws from a multiprocessing queue, and I found that when I run this code, I get data = []
. Whereas if I tell the program to sleep for a little bit at the place denoted, I get data = [1,2]
, as it should.
from multiprocessing import Queue
import time
q = Queue()
taken = 0
data = []
for i in [1,2]:
q.put(i)
# time.sleep(1) making this call causes the correct output
while not q.empty() and taken < 2:
try:
data.append(q.get(timeout=1))
taken+=1
except Empty:
continue
**EDIT:**This also happens if there's a print statement before the while loop. This suggests to me that there's something happening with the calls to q.put() that's happening, but I can't find any documentation on this issue.
It's mentioned in the docs for multiprocessing.Queue