Queue.get_nowait() sometimes bogusly returns Empty

Tim Peters tim.one at comcast.net
Tue Jul 16 18:44:55 EDT 2002


[Geoffrey Talvola]
> Now that I think of it, maybe a Queue just isn't the right tool for this
> job.  Queues are basically designed for the case where you want
> block on an empty queue.

Also to block put() on a full Queue -- bounded queues are a natural approach
to mediating between producers and consumers with different rates of
production and consumption.

> But for this use I never want blocking.  Perhaps a simple
> combination of a list and a lock would be better, or even a list alone
> without a lock:
>
> 	try:
> 		instance = cachelist.pop()
> 	except IndexError:
> 		instance = new_instance()
> 	# use the instance
> 	cachelist.append(instance)
>
> As long as pop() and append() are atomic, which I believe they are, then
> this ought to work.

They're atomic, yes.  If the number of instances can't outnumber the number
of threads, though, then a dead simple approach is to give each thread its
own instance at start and skip all this queue fiddling.






More information about the Python-list mailing list