Not fully understanding the role of Queue.task_done()

castironpi castironpi at gmail.com
Thu Sep 4 22:04:54 CEST 2008


On Sep 4, 2:51 pm, Martin DeMello <martindeme... at gmail.com> wrote:
> On Sep 4, 12:41 pm, Fredrik Lundh <fred... at pythonware.com> wrote:
>
> > "task_done" just decrements a counter (incremented by "put").  when the
> > counter reaches zero, the "join" call is unblocked.
>
> Thanks! Is there any standard python idiom to empty a queue into a
> list? Or do I just call get() repeatedly and catch the exception when
> it's done?
>
> martin

Random access isn't supported by the defined interface.  You can make
it more convenient, though.

import Queue

class IterQueue( Queue.Queue ):
    def __iter__( self ):
        return self
    def next( self ):
        if self.empty():
            raise StopIteration
        return self.get()

q= IterQueue()
q.put( 'a' )
q.put( 'b' )
q.put( 'c' )

print [ x for x in q ]

/Output:
['a', 'b', 'c']



More information about the Python-list mailing list