[Python-3000] Useless methods in Queue module

Mike Klaas mike.klaas at gmail.com
Tue Jan 15 03:31:08 CET 2008


On 14-Jan-08, at 5:49 PM, Jeffrey Yasskin wrote:

> On Jan 14, 2008 2:55 PM, Mike Klaas <mike.klaas at gmail.com> wrote:

>> This is a misleading name.  The number returned from qsize() [not  
>> size
>> ()] is perfectly reliable, in the sense that it is the exact size of
>> the queue at some instant in time.
>
> No, you're wrong. In CPython's implementation, it's likely that qsize
> is implemented by locking the structure and reading a size field,

It isn't likely, it is.  It is also implemented as such in all  
mainstream python interpreters (ironpython, jython), since they use  
CPython's pure-python implementation.

I grant that it is theoretically possible to implement Queue.Queue's  
full interface with a data structure in which qsize() is never a  
consistent length of the index at a given time, even if such a  
structure may not yet exist.

> Furthermore, it's misleading to think of even a locked
> implementation's result as reliable. Ignoring problems with
> simultaneity in the presence of threads, the "true" value could easily
> have changed by the time qsize returns. And if it returns a value
> that's not accurate, that can hardly be described as reliable. It
> really is returning an estimate of the size, suitable only for use in
> performance optimizations where you're prepared to handle it sometimes
> being wildly wrong.

In some usage patterns it is thread-unsafe.  Whether this means that  
the method itself is "unreliable" is a battle likely not worth  
getting in to.

Thanks for the interesting reference,
-Mike


More information about the Python-3000 mailing list