Skip to content

Instantly share code, notes, and snippets.

@glaucocustodio
Last active March 29, 2022 09:28
Show Gist options
  • Save glaucocustodio/1c91060b8f0d27c32f2e2ba5f1c9c4b7 to your computer and use it in GitHub Desktop.
Save glaucocustodio/1c91060b8f0d27c32f2e2ba5f1c9c4b7 to your computer and use it in GitHub Desktop.
FixedThreadPool / Concurrent::Future usage example (concurrent-ruby gem)
# For more: https://github.com/ruby-concurrency/concurrent-ruby
num_threads = Concurrent.processor_count # or whatever you prefer like 4
thread_pool = Concurrent::FixedThreadPool.new(num_threads)
products = Product.all
executors = products.map { |product|
Concurrent::Future.execute({executor: thread_pool}) do
p "processing #{product.id}"
end
}
executors.map(&:value!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment