Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't Queue.qsize accurate?

Tags:

python

queue

According to the Python 2.7 docs, Queue.qsize isn't dependable, and help(Queue.Queue.qsize) says that it isn't reliable. Is there a particular implementation issue I am not aware of?

P.S. I am aware that Queue.Queue.qsize uses mutexes, and that the size of the Queue may change between when I call the method and when I get the result, but for single-threaded applications, are Queues safe?

Message from help(Queue.Queue.qsize):

>>> help(Queue.Queue.qsize)
Help on method qsize in module Queue:

qsize(self) unbound Queue.Queue method
    Return the approximate size of the queue (not reliable!).

>>> 
like image 459
Snakes and Coffee Avatar asked Jan 14 '13 05:01

Snakes and Coffee


People also ask

What does the value returned by the Qsize () function of a queue represent?

The method qsize() returns the number of elements present in a queue.

How do I find the size of a python queue?

To get the length of a queue in Python:Use the len() function to get the length of a deque object. Use the qsize() method to get the length of a queue object.


1 Answers

Queue.Queue.qsize works fine in a single-threaded application (and even in a multi-threaded application for many applications of its purpose). You simply can't use it to reliably determine whether a call to put or get will block.

Note that if you don't need concurrency, collections.deque is faster than Queue.Queue. Or, if performance isn't critical, you could go the simple route and just use regular lists.

like image 107
Taymon Avatar answered Sep 23 '22 19:09

Taymon