Re: Why are "broken iterators" broken?

Discussion in 'Python' started by Cameron Simpson, Sep 22, 2008.

  1. On 21Sep2008 18:36, Fredrik Lundh <> wrote:
    > Roy Smith wrote:
    >> There are plausible examples of collections which grow while you're
    >> iterating over them. I'm thinking specifically of a queue in a
    >> multi-threaded application. One thread pushes work onto the back of
    >> the queue while another pops from the front. The queue could certainly
    >> go empty at times. But, maybe a Python iterator is just the wrong way
    >> to model such behavior.

    >
    > you probably want the consumer thread to block when it catches up with
    > the producer, rather than exit.
    > (that's the default behaviour of Python's Queue object, btw)


    It sounds like he wants non-blocking behaviour in his consumer.
    A common example is "try to gather a lot of stuff into a single packet,
    but send a smaller packet promptly if there isn't much stuff".

    You could make the next() method return a sentinal value like None
    when the queue is empty. That would mean your consumer must recognise
    the special value and also precludes delivering that value through the
    queue. I'm not convinced my suggestion here is any better than just
    doubling up every call to next() with an empty() check immediately
    beforehand.

    You could write a trivial wrapping generator to take the original
    blocking queue and return a sentinel value on empty, too.

    My suggestion is also an excellent way of getting programs that
    fail-busy (i.e. they spin out) if you make a logic error in your
    consumer. Ouch.

    Cheers,
    --
    Cameron Simpson <> DoD#743
    http://www.cskk.ezoshosting.com/cs/

    Kill, v.t. To create a vacancy without nominating a successor.
    Ambrose Bierce (1842-1914), U.S. author. The Devil's Dictionary (1881-1906).
     
    Cameron Simpson, Sep 22, 2008
    #1
    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. =?Utf-8?B?VGltOjouLg==?=

    Why, why, why???

    =?Utf-8?B?VGltOjouLg==?=, Jan 27, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    589
    Juan T. Llibre
    Jan 27, 2005
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    933
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,128
    Smokey Grindel
    Dec 2, 2006
  4. Steven D'Aprano

    Why are "broken iterators" broken?

    Steven D'Aprano, Sep 21, 2008, in forum: Python
    Replies:
    8
    Views:
    683
  5. Fredrik Lundh

    Re: Why are "broken iterators" broken?

    Fredrik Lundh, Sep 22, 2008, in forum: Python
    Replies:
    0
    Views:
    622
    Fredrik Lundh
    Sep 22, 2008
Loading...

Share This Page