The built-in function len()
(https://docs.python.org/3/library/functions.html#len) returns "the length (the number of items) of an object", but this is not implemented for queue.Queue
(https://docs.python.org/3/library/queue.html).
Instead, queue.Queue
has a qsize()
method which returns the approximate size of a queue, when it clearly has a length; you can specify the maximum length of a Queue
in the constructor. The similar collections.deque
does work with len
.
What are the reasons for not using the common len()
for queue.Queue
? Or: What would be the problems if qsize
were instead named __len__
to enable the len()
function?
len()
isn't implemented for queue.Queue
because it would be an "attractive nuisance": something that only an expert should even consider using, but a "friendly name" would encourage non-experts to use it.
Unlike most sequence types (like list
and deque
), a queue.Queue
is specifically intended to be used in multi-threaded contexts (and similarly for the multiprocessing
module's queue type). While the number of items in a Queue
certainly has a definite value at any particular time, it's impossible for user code to find out what that value is: between the time a call to .qsize()
returns and your code can look at the returned value, any number of other threads (or processes, in the multiprocessing
case) may have made any number of changes to the queue's contents.
So the only true thing that can be said about the value returned by .qsize()
is that the Queue
had that many values in it at some time in the past. By the time you can use the returned value, it may have arbitrarily more (or fewer) values in it.
Of course that's not so if you're only running one thread - but then there's no need to pay for the implementation complexity of a Queue
(use a list
or a deque
instead).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With