why the method get() of python Queue is hang on there?

Tim Chase python.list at tim.thechases.com
Mon Aug 14 11:29:09 EDT 2006


 > import Queue
 > b = Queue.Queue(0)
 > b.put(9999)
 > b.get()   # this is ok, it pops out 9999
> b.get() # this one does not return anything and is hang on
> there
> 
> Anybody knows what is going on with the second b.get()?

 >>> help(Queue.Queue)
:
:
|  get(self, block=True, timeout=None)
|      Remove and return an item from the queue.
|
|      If optional args 'block' is true and
|      'timeout' is None (the default), block if
|      necessary until an item is available. If
|      'timeout' is a positive number, it blocks
|      at most 'timeout' seconds and raises the
|      Empty exception if no item was available
|      within that time.  Otherwise ('block' is
|      false), return an item if one is
|      immediately available, else raise the
|      Empty exception ('timeout' is ignored in
|      that case).
|
|  get_nowait(self)
|      Remove and return an item from the queue
|      without blocking.
|
|      Only get an item if one is immediately
|      available. Otherwise raise the Empty
|      exception.
:
:

Notice that by default, get() will block until
there's something to read.  You can use either

	b.get(block=False)

or

	b.get_nowait()

In either case, an exception will be raised when
the Queue is empty, to let you know as much.  Just
trap for the exception and do as you please.

-tkc







More information about the Python-list mailing list