Queue enhancement suggestion

Antoon Pardon apardon at forel.vub.ac.be
Mon Apr 16 05:20:24 EDT 2007


On 2007-04-16, Paul Rubin <http> wrote:
> I'd like to suggest adding a new operation
>
>    Queue.finish()
>
> This puts a special sentinel object on the queue.  The sentinel
> travels through the queue like any other object, however, when
> q.get() encounters the sentinel, it raises StopIteration instead
> of returning the sentinel.  It does not remove the sentinel from
> the queue, so further calls to q.get also raise StopIteration.
> That permits writing the typical "worker thread" as
>
>    for item in iter(q.get): ...
>

The problem is this doesn't work well if you have multiple producers.
One producer can be finished while the other is still putting values
on the queue.

The solution I have been thinking on is the following.

Add an open and close operation. Only threads that have the queue
open can access it. The open call should specify whether you
want to read or write to the queue or both. When all writers
have closed the queue and the queue is empty a q.get will
raise an exception. This may be done by putting a sentinel
on the queue when the last writer closed the queue.

-- 
Antoon Pardon



More information about the Python-list mailing list