[Python-ideas] `__iter__` for queues?

Stefan Behnel stefan_ml at behnel.de
Wed Jan 20 20:03:58 CET 2010

MRAB, 20.01.2010 00:01:
> Georg Brandl wrote:
>> Your obvious queue iterator would call get(block=False) and stop on
>> Empty. The other obvious meaning is be to call get(block=True) forever.
>> IMO they are both too "obvious" to make a call -- an explicit while loop
>> is better.

I would have expected the behaviour to be infinite/blocking, so I guess I'm
+1 on the "non-obvious" bit.

> To me the 'obvious' meaning is to call get(block=True) and have it raise
> Empty (actually, StopIteration) when the queue is empty and the 'sender'
> has somehow signalled that no more items will be put into the queue
> (q.finished()?). This would also eliminate the need for a sentinel!

You could always use

    for item in iter(my_queue, THE_SENTINEL):

I think that's simple enough.


More information about the Python-ideas mailing list