Queue.get_nowait() sometimes bogusly returns Empty

Geoffrey Talvola gtalvola at nameconnector.com
Mon Jul 15 18:45:02 EDT 2002


Queue doesn't seem to support the operation "if the queue is empty, raise
the Empty exception, otherwise remove and return an item from the queue".
There is Queue.get_nowait but unfortunately, that sometimes raises the Empty
exception even if the Queue is not empty.  Sure smells like a bug to me, but
this topic has come up before and Tim Peters sort of declared that it was
the way it's supposed to work:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&th=ddd2f958ddd08f95&rnum=
2

Leaving aside the question of whether this is reasonable behavior, suppose
that I actually did want the behavior I described above -- a reliable "if
the queue is empty, raise the Empty exception, otherwise remove and return
an item from the queue".  How would I write it?  Do I need to busy-loop like
this (untested):

	def get_nowait_reliable(self):
		while 1:
			try:
				return self.get_nowait()
			except Empty:
				# Let's see if it's really empty.  If not,
try again.
				if self.empty():
					raise

or is there a better way?

- Geoff





More information about the Python-list mailing list