5

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

>>> 
Snakes and Coffee
  • 8,747
  • 4
  • 40
  • 60

1 Answers1

10

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.

Taymon
  • 24,950
  • 9
  • 62
  • 84
  • Not sure what you mean by "_you don't need concurrency_", the documentation on deque says: "_Deques support thread-safe, memory efficient appends and pops..._". Can you clarify? – totaam Jul 30 '14 at 09:45