通过使用 qsize()
可以知道队列的大小
- How do we know the max size of a queue? Or in other way, when the condition
queue.full()
will returnTrue
? Why is the result for
q.put(i)
always resulting inNone
whereasq.get(i)
is resulting in corresponding value?from queue import Queue q = Queue() print("------Queue info when putting -----") for i in range(5): print("---Loop---- :",i) print("queue empty : ", q.empty()) print("put : ",q.put(i)) print("queue size : ", q.qsize()) print("queue full : ", q.full()) print("------Queue info when getting -----") for i in range(5): print("---Loop---- :",i) print("get : ",q.get(i)) print("task done : ",q.task_done()) print("queue empty : ", q.empty())
------Queue info when putting -----
---Loop---- : 0
queue empty : True
put : None
queue size : 1
queue full : False
(loop continues for 5 times)
------Queue info when getting -----
---Loop---- : 0
get : 0
task done : None
queue empty : False
(loop continues for 5 times)
put(item,False)
或等效的put_nowait(item)
怎么办? 它不会等待该项被消耗,而只会引发“Full”。 因此,总体而言,它并不是无限的。 - Adrian