Why is len() not implemented for Queues? Why is len() not implemented for Queues? multithreading multithreading

Why is len() not implemented for Queues?


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).