bug with Queue in Python 1.5.2

Tim Peters tim.one at home.com
Fri Apr 20 17:58:25 EDT 2001


[Alexandre Fayolle]
> I think I encountered a bug in Queue using python 1.5.2, and I was
> wondering if it was still around in python 2.x.
>
> I have a Queue with infinite size

No you don't <wink>.

> that sometimes raises QueueFull exceptions when I use put_nowait.
>
> The reason is that the fsema lock is acquired before the mutex lock,
> and it is possible for instance with 2 threads to have the first one
> take fsema and mutex, and then the second one trying to acquire fsema,
> and failing immediately (because it's in nowait mode), which causes a
> Full Queue exception.
>
> Is this still the case with later releases of Python?

Yes, it is, although the docs may have changed in the meantime.  Here's the
relevant bit from the 2.1 docs:

exception Full
    Exception raised when non-blocking put() (or put_nowait()) is
    called on a Queue object which is full or locked.

That is, Full doesn't mean *only* full.  If you're playing with non-blocking
puts or gets, the reasonable presumption is that you want to regain control
ASAP if the Queue can't satisfy your request immediately.  If the Queue is
locked at the time of the call, your request can't be satisfied immediately
for that reason alone, and so the call returns ASAP but without doing
anything (and raises an exception to let you *know* it didn't do anything).

In your case, since your Queue is infinite, it doesn't make sense to use a
non-blocking put, unless you *want* Queue to raise Full when it's locked(!):
use plain .put() instead.  Then you'll never see Full.





More information about the Python-list mailing list