Skip to content

Instantly share code, notes, and snippets.

@rda1902
Forked from sixty4bit/ruby_threads_forks.rb
Created February 14, 2022 08:05
Show Gist options
  • Select an option

  • Save rda1902/f3b7b4d017c55b7cffbd390fc50f2d82 to your computer and use it in GitHub Desktop.

Select an option

Save rda1902/f3b7b4d017c55b7cffbd390fc50f2d82 to your computer and use it in GitHub Desktop.
batches = Queue.new
Order.select(:id).find_in_batches do |batch|
batches << batch.map(&:id)
end
cpu_available.times do
Thread.new do
while(ids = batches.pop)
pid = Process.fork do
Sunspot.index! Order.includes(:product).find(ids)
end
Process.wait(pid)
end
end
end
Thread.new do
sleep 1 until batches.size.zero?
end.join
Process.waitall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment