iterator interface for Queue?

Discussion in 'Python' started by Paul Rubin, Apr 8, 2007.

  1. Paul Rubin

    Paul Rubin Guest

    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.
    Paul Rubin, Apr 8, 2007
    #1
    1. Advertising

  2. Paul Rubin

    Leo Kislov Guest

    On Apr 7, 11:40 pm, Paul Rubin <http://> 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
    Leo Kislov, Apr 8, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul L. Du Bois

    Queue.Queue-like class without the busy-wait

    Paul L. Du Bois, Mar 24, 2005, in forum: Python
    Replies:
    29
    Views:
    1,037
    Antoon Pardon
    Apr 4, 2005
  2. Russell Warren

    Is Queue.Queue.queue.clear() thread-safe?

    Russell Warren, Jun 22, 2006, in forum: Python
    Replies:
    4
    Views:
    662
    Russell Warren
    Jun 27, 2006
  3. Kceiw
    Replies:
    3
    Views:
    979
    Jim Langston
    Mar 14, 2006
  4. Gabriel Rossetti
    Replies:
    3
    Views:
    532
    Jerry Hill
    Apr 25, 2008
  5. Kris
    Replies:
    0
    Views:
    461
Loading...

Share This Page