Why does this queue behave like this?

2020-05-06 11:35发布

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.

1条回答
Deceive 欺骗
2楼-- · 2020-05-06 11:50

It's mentioned in the docs for multiprocessing.Queue

Note When an object is put on a queue, the object is pickled and a background thread later flushes the pickled data to an underlying pipe. This has some consequences which are a little surprising, but should not cause any practical difficulties – if they really bother you then you can instead use a queue created with a manager.

  1. After putting an object on an empty queue there may be an infinitesimal delay before the queue’s empty() method returns False and get_nowait() can return without raising Queue.Empty. ...
查看更多
登录 后发表回答