Queue qsize = unreliable?

Jeff Shannon jeff at ccvcorp.com
Fri Aug 6 23:22:46 CEST 2004

Ames Andreas (MPA/DF) wrote:

>Tim Peters wrote:
>>There's no use case I know of where knowing "the exact current size"
>>is necessary, except inside the implementation of Queue (where it
>>can know it, but at the expense of locking out all other threads for
>>as long as it takes to compute the result).
>I've got one:  If you have the special situation that there is only a
>single consumer with potentially multiple suppliers (or similarly if
>you have it the other way around), you could implement the consumer in
>a non-blocking manner.  Then it's not that important to get "the exact
>current size" which would still be unreliable but it is important to
>have a reliable empty method (or rather !empty() and full() are
>reliable in this situation).  I think Queue is reliable in this
>respect but to get it additionally non-blocking you will have to roll
>your own queue.

No you don't.  You simply execute a non-blocking get(),  and be prepared 
to catch the Queue.Empty exception.  Similarly, if you want a 
non-blocking producer, then you execute a non-blocking put() and catch 
the Queue.Full exception.  That's really the only reliable way to tell 
whether the queue is empty or full, anyhow...

Jeff Shannon
Credit International

More information about the Python-list mailing list