Vincent Fourmond
9/27/2006 8:22:00 PM
Jordan McKible wrote:
> Vincent Fourmond wrote:
>> class Filler
>>
>> def refill
>> ary = [pull the feeds]
>> for element in ar
>> queue << element
>> end
>> sleep...
>> end
>> end
>
> I don't think this is quite what I need. The worker threads should
> never exit - they should just sleep until the queue is refilled.
This is exactly what this scheme is doing. I was just mentionning the
exit so you give all the worker threads a chance to exit if the program
ever stops. I bet it will stop, one day, won't it ? The queue.pop is
*blocking*, which means it will wait until the queue has some elements:
-------------------------------------------------------------- Queue#pop
pop(non_block=false)
------------------------------------------------------------------------
Retrieves data from the queue. If the queue is empty, the calling
thread is suspended until data is pushed onto the queue. If
non_block is true, the thread isn't suspended, and an exception is
raised.
It
> seems like this configuration could lead to the queue being filled up
> before it's completely depleated by workers.
You can't fill up the queue (unless you mean run out of memory ?). And
you can use SizedQueue if you're worried about it: you set a limit on
the number of elements in the queue. Then, when that number is reached, the
queue << elements
blocks. It wakes up again when workers have depleted it enough.
I know this scheme works, as I use it for a massive parallel download
program: I build a queue of elements to download, and I use several
threads to pull them. Works perfectly. This really is exactly what you
need: no need to bother about synchronisation whatsoever...
Cheers !
Vince