Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Why are "broken iterators" broken?

Thread Tools

Re: Why are "broken iterators" broken?

Cameron Simpson
Posts: n/a
On 21Sep2008 18:36, Fredrik Lundh <(E-Mail Removed)> 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

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.

Cameron Simpson <(E-Mail Removed)> DoD#743

Kill, v.t. To create a vacancy without nominating a successor.
Ambrose Bierce (1842-1914), U.S. author. The Devil's Dictionary (1881-1906).
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM