I have a method in a Ruby On Rails application that I
If you start the worker threads before filling up the queue, they will start consuming the queue as you fill it up, and because as a rule of thumb - network is slower than CPU, each batch should be (mostly) consumed by the time the next batch arrives:
queue = Queue.new
t1 = Thread.new do
while !queue.empty?
p queue.pop(true)
sleep(0.1)
end
end
t2 = Thread.new do
while !queue.empty?
p queue.pop(true)
sleep(0.1)
end
end
(0..1000).map(&queue.method(:push))
t1.join
t2.join
If that proves too slow still, you can opt to use SizedQueue, which will block the push
if the queue reaches a big enough size:
queue = SizedQueue.new(100)
t1 = Thread.new do
while !queue.empty?
p "#{queue.pop(true)} - #{queue.size}"
sleep(0.1)
end
end
t2 = Thread.new do
while !queue.empty?
p queue.pop(true)
sleep(0.1)
end
end
(0..300).map(&queue.method(:push))
t1.join
t2.join