TaskQueue

Rene Pijlman reply.in.the.newsgroup at my.address.is.invalid
Wed Mar 22 01:57:41 CET 2006


Carl Banks:
>Rene Pijlman:
>> Ah yes, clever trick. But you'd have to worry about thread-safety of your
>> subclass though.
>
>Queue worries about this for you.  The Queue class only calls _get when
>it has the queue's mutex, so you can assume thread safety when
>subclassing it.

Ah yes, I overlooked the underscore. But you'd have to worry about
everything get() is doing before it calls _get() though :-)

>Say you were writing a FiniteQueue class that implemented the permament
>sentinel trick to indicate to all consumer threads that the queue is
>done.  For your own code, you may know that you'll never need to pass
>None through the queue, but you can't assume that for all code.

Ah, this is where assert proves its value.

class FiniteQueue(object):
    def putTask(self,task):
        assert task is not None
        self.workQueue.put(task)
        self.numberOfTasks += 1
    # ...

-- 
René Pijlman

Wat wil jij leren?  http://www.leren.nl



More information about the Python-list mailing list