Queue enhancement suggestion
apardon at forel.vub.ac.be
Tue Apr 17 02:39:42 EDT 2007
On 2007-04-17, Paul Rubin <http> wrote:
> Antoon Pardon <apardon at forel.vub.ac.be> writes:
>> 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.
> Right, you'd wait for all the producers to finish, then finish the queue:
> for p in producer_threads: p.join()
>> 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.
> That's an idea, but why would readers need to open the queue?
That has to do with how I implemented it. My implementation
puts n sentinels on the queue when there are n readers at the
moment the last writer closes the queue.
I also treat writers differently from read-writers. Read-writers
never block, while writers can be blocked if the queue is "full".
More information about the Python-list