python : how to know max size of a queue
Solution 1
To answer your 1st question: for all intents and purposes, the max size of a Queue is infinite. The reason why is that if you try to put something in a Queue that is full, it will wait until a slot has opened up before it puts the next item into the queue. This is assuming you initialize the Queue with the max size parameter. You can see here: class queue.Queue(maxsize)
that you can pass a maxsize parameter. According to the docs: " If maxsize is less than or equal to zero, the queue size is infinite."
This leads to the second question: the put()
method puts the item into the queue and returns None
if it was successful. From the documentation,
put(item[, block[, timeout]])
If timeout is a positive number, it blocks at most timeout seconds and raises the Full exception if no free slot was available within that time. Otherwise (block is false), put an item on the queue if a free slot is immediately available, else raise the Full exception (timeout is ignored in that case).
Therefore, when you put something into the queue, it will always return None
unless an exception was raised. get()
, on the other hand, returns the item it gets. From the docs again:
get([block[, timeout]])
: Remove and return an item from the queue.
Solution 2
Check out the source code for the implementation of Queue: https://hg.python.org/cpython/file/3.5/Lib/queue.py
As you can see from the init function, 'queue.maxsize' is a member variable that should contain the max size of the queue.
You can therefore do something like this:
from queue import Queue
my_queue = Queue(maxsize=10)
max_size = queue.maxsize
Comments
-
Here_2_learn almost 2 years
Size of a queue can be known by using
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())
output is as follows :
------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)
- How do we know the max size of a queue? Or in other way, when the condition
-
Adrian about 4 years
The reason why is that if you try to put something in a Queue that is full, it will wait until a slot has opened up before it puts the next item into the queue
. What aboutput(item, False)
or the equivalentput_nowait(item)
? It won't wait until an item is consumed, it will just raiseFull
. So, no, it's not infinite for all intents and purposes.