Skip to content

Instantly share code, notes, and snippets.

@carpodaster
Last active January 16, 2019 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carpodaster/af6b142fbc1246714ef92d97147e0926 to your computer and use it in GitHub Desktop.
Save carpodaster/af6b142fbc1246714ef92d97147e0926 to your computer and use it in GitHub Desktop.
require 'thread'
WORKERS = 3
queue = SizedQueue.new(WORKERS * 2)
# Backlog some work! Using a separate thread since push operation on
# SizedQueue may block when it's "full"
Thread.new { MyModel.find_in_batches { |batch| queue << batch } }
readio, writeio = IO.pipe
# This is the worker pool. Create a fixed number of threads to get work
# (ie. an ActiveRelation batch) off the queue and run the CSV transformation
exporters = 1.upto(WORKERS).map do
Thread.new {
until queue.empty? do
group = queue.pop
group.each { |record| writeio.write MyCSVTransformation.call(record) }
end
}
end
# IO "guard" cleans up when all exporter threads have finished their work
Thread.new {
while exporters.any?(&:alive?) do sleep 0.01 end
writeio.close
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment