问题
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