[Python-ideas] `__iter__` for queues?

Raymond Hettinger raymond.hettinger at gmail.com
Wed Jan 20 01:08:29 CET 2010

On Jan 19, 2010, at 1:10 PM, Simon Brunning wrote:

> 2010/1/19 cool-RR <cool-rr at cool-rr.com>:
>> Is there a reason that queues don't have an `__iter__` method? I mean both
>> `Queue.Queue` and `multiprocessing.Queue`.
> Could it be made threadsafe?

To me, this is exactly the right line of questioning.

Is there a use case for iterating through a queue
that is being mutated by consumers and producers?
If so, should the consumers and producers be locked
out during iteration or should the iterator fail if it detects
a mutation (like we currently do for dictionaries that
change during iteration).

The semantics should be dictated by use cases.
When someone writes "for t in q: action(q)" what
is the most useful thing to happen when another
thread does a get or put?  

My personal opinion is that queues are better-off without
an iterator.  They serve mainly to buffer consumers and
producers and iteration does fit in well.  In other words,
adding __iter__ to queues in threaded environment
may do more harm than good. 


More information about the Python-ideas mailing list