
On 1/19/2010 4:20 PM, cool-RR wrote:
On Tue, Jan 19, 2010 at 11:10 PM, Simon Brunning <simon@brunningonline.net <mailto:simon@brunningonline.net>> wrote:
2010/1/19 cool-RR <cool-rr@cool-rr.com <mailto:cool-rr@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?
-- Cheers, Simon B.
For me, iterating on the queue means just calling `get` repeatedly until it's empty.
Explicit .get()s release the queue between getting each item so other code can also put and get. A mutable collection iterator may or may not lock the collection or monitor mutations. Dict iteration (and, I presume, set iteration, but I see nothing in the doc) monitors mutation. List iteration does not. While modifying a list during iteration can have uses, it is also leads to bugs and newby confusion. On the otherhand, one can write explicitly iterate dicts with while d: k,v = dict.getitem() <go ahead and modify dict> and the same with set.pop and have no problem. So iterating with an iterator is not quite the same as repeated one-item fetches. Terry Jan Reedy