Queue.get_nowait() sometimes bogusly returns Empty

Geoffrey Talvola gtalvola at nameconnector.com
Wed Jul 17 09:32:30 EDT 2002


Tim Peters wrote:
> [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.

Yes, the app server does make use of a bounded queue to mediate the overall
request handling, but not to mediate the doling out of _specific_ types of
requests which is the code in question here.

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

The appserver doesn't actually know what all the possible servlets are at
startup -- they are just stored in the filesystem and can be added or
modified while the appserver is running.  And it seems wasteful to allocate
20 instances when 1 will do...

I went with the cachelist solution above and so far it's working nicely.

Thanks for the help!

- Geoff





More information about the Python-list mailing list