iterator interface for Queue?

Leo Kislov Leo.Kislov at gmail.com
Sun Apr 8 07:09:05 EDT 2007


On Apr 7, 11:40 pm, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> Is there any reason Queue shouldn't have an iterator interface?
> I.e. instead of
>
>     while True:
>        item = work_queue.get()
>        if item is quit_sentinel:
>            # put sentinel back so other readers can find it
>            work_queue.put(quit_sentinel)  
>            break
>        process(item)

It's almost equal to:

for item in iter(work_queue.get, quit_sentinel):
    process(item)

except that it doesn't keep the quit sentinel in the queue. But that's
a personal preference, I usually put as many quit sentinels in a queue
as many consumers.

  -- Leo




More information about the Python-list mailing list