Py Queue library - mapping array to queue - py3 issues

醉酒当歌 提交于 2019-12-11 04:52:42

问题


I previously posted a question regarding a script which performed conccurent requests. This script was failing, because of an empty queue.

I further investigated the problem and came with this simple problem. We have a function which creates a queue based on an array. We then iterate over the queue, printing the value, until the queue is empty.

try:
  from queue import Queue
except:
  pass
try: 
  from Queue import Queue
except:
  pass

def test():
  q = Queue()
  a = [1,2,3]
  map(q.put, a)
  print("queue size after insertion: ", str(q.qsize()))
  while not q.empty():
    item = q.get()   
    print(item)
     

if __name__ == "__main__":
  print("Starting test.")
  test()
  print("Test finished.")

In python2, everything works fine. The queue size after insertion is 3

In python3, after insertion, the queue size is 0, and thus the iteration over the queue is not being executed.

SOLUTION: changing the insertion method fixed the problem. replace:

map(q.put, a)

for:

 for item in a:
     q.put(item)

I wonder what causes this problem with the map function in python3.

来源:https://stackoverflow.com/questions/49039676/py-queue-library-mapping-array-to-queue-py3-issues

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