473,395 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Re: Why are "broken iterators" broken?

On 21Sep2008 18:36, Fredrik Lundh <fr*****@pythonware.comwrote:
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 <cs@zip.com.auDoD#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).
Sep 22 '08 #1
0 938

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: beliavsky | last post by:
I just came across the slides for Guido van Rossum's "Python Regrets" talk, given in 2002. It worries me that much of my Python code would be broken if all of his ideas were implemented. He doesn't...
205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
1
by: putty | last post by:
I found a few posts of people asking about insertCell()/insertRow() not working in IE6 SP2, and a few others about getting "null is null or not an object" errors, but no one posted a solution...
3
by: ITP | last post by:
Hello, I'm seeing this with the ADO.NET SQLClient provider: When I check the connection's open status after it has been opened AND has been broken (I simulate this by disconnecting the cable...
6
by: marcwentink | last post by:
Dear Sirs, Dear Newsgroup, Imagine I have some function that only gives me an iterator to a vector, but not the vector itself. Unfortunately this vector can be empty and the iterator can point...
2
by: Samuel | last post by:
Hi, When using telnetlib, the connection sometimes breaks with the following error: "error: (32, 'Broken pipe')" where the traceback points to self.sock.send(buffer)
8
by: Steven D'Aprano | last post by:
According to the Python docs, once an iterator raises StopIteration, it should continue to raise StopIteration forever. Iterators that fail to behave in this fashion are deemed to be "broken": ...
0
by: Fredrik Lundh | last post by:
Cameron Simpson wrote: Roy gave an example, he didn't post a requirements specification. that use case is better solved with a plain list object. no need to make things harder than they...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.