Queue qsize = unreliable?

Mike C. Fletcher mcfletch at rogers.com
Fri Aug 6 17:33:05 CEST 2004

James R. Saker Jr. wrote:

>I see per pydoc that Queue.Queue()'s .qsize is allegedly unreliable:
>     |  qsize(self)
>     |      Return the approximate size of the queue (not reliable!).
>Any thoughts on why this is unreliable (and more curiously, why it would
>be put in there as an unreliable function?) Rather than roll my own
>threaded fifo class, it would seem prudent to use Python's built-in
>Queue but the warning signs on a rather necessary function seem curious.
I would imagine that there is the potential for an item to be added or 
consumed during the running of the qsize method, or between the 
completion of the running of the qsize method and the op-codes which 
deal with the result.

That is, what you're getting is the size of the queue from a few 
milliseconds ago, which may have *no* relationship to the current size.  
It is "unreliable" in the sense that you shouldn't do something like "if 
q.qsize(): q.pop()"  if you will fail when q.pop() blocks with an empty 
queue.  It's useful in cases where you're trying to see how large a 
large queue is, but it's not something you want to be mucking about with 
to try to create new low-level thread-safe primitives.

Tim, of course, would know better, but that's always been my interpretation.

Have fun,

  Mike C. Fletcher
  Designer, VR Plumber, Coder

More information about the Python-list mailing list