473,287 Members | 1,616 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,287 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 934

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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.