TaskQueue

Carl Banks invalidemail at aerojockey.com
Wed Mar 22 01:23:54 CET 2006


Rene Pijlman wrote:
> Carl Banks:
> >Rene Pijlman:
> >>         for i in range(self.numberOfThreads):
> >>             self.workQueue.put(None)
> >
> >Or, you could just put one sentinel in the Queue, and subclass the
> >Queue's _get method not to take the sentinel out.
>
> 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.


> >BTW, for sentinels, I recommend creating one using:
> >
> >sentinel = object()
> >
> >Because, although it's not applicable to your example, sometimes None
> >is an object you want to pass through.  (I think Alex Martelli came up
> >with that one.)
>
> You mean this post, I guess:
> http://mail.python.org/pipermail/python-list/2004-October/244750.html
>
> I dunno, but I cannot think of a better way to say "this has no possible
> use" than to pass None.

The problem is, sometimes None has a possible use.  In your example I
don't think it's a big deal; you're using None more like a "null
pointer" than a "sentinel" anyways.  But it's not a good idea for a
generic implementation.

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.


> Place yourself in the position of someone who
> doesn't know exactly what object() is and who encounters it in the code.
> I've just spent 5 minutes trying to find some reference information and I
> gave up ("object" is a lousy search word).

I would hope the word "sentinel" in "sentinel = object()" would give
that person some clue as to what it is. :)


Carl Banks




More information about the Python-list mailing list