[Python-ideas] `__iter__` for queues?

Anh Hai Trinh anh.hai.trinh at gmail.com
Wed Jan 20 14:54:52 CET 2010


> I will note that the check-and-reinsert example given elsewhere in this
> thread contains a race condition in the multiple producer use case or in
> cases where a single producer may place additional items in the queue
> after the shutdown sentinel:
>
> def _iterqueue(queue):
>  while 1:
>    item = queue.get()
>    # Queue is not locked here, producers may insert more items
>    # and other consumers may process items that were in the queue
>    # after the sentinel
>    if item is StopIteration:
>      # We put StopIteration into the queue again, but it may not be
>      # at the front if a producer inserted something after the original
>      # insertion of the sentinel value
>      queue.put(StopIteration)
>      break
>    else:
>      yield item

You are quite right. The assumption is that the StopIteration
singleton correctly marks the end of the queue however that is
achieved (e.g. have the producer-spawning thread join() with all
producers, then put StopIteration).

Cheers,

-- 
// aht
http://blog.onideas.ws



More information about the Python-ideas mailing list