Why does this queue behave like this?

安稳与你 提交于 2019-12-20 06:47:34

问题


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:


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. ...


来源:https://stackoverflow.com/questions/34212603/why-does-this-queue-behave-like-this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!