iterator interface for Queue?

P

Paul Rubin

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)

with accompanying hair on the other side to create and send a sentinel,
you'd just say:

for item in work_queue:
process(item)

You'd still need hair at the writing end to tell the readers when to
finish, either by directly inserting a sentinel or with
"work_queue.done()" or something like that.
 
L

Leo Kislov

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top