RactorPool
A thread-safe, lock-free pool of Ractor workers with a coordinator pattern for distributing work.
Installation
Install the gem and add to the application’s Gemfile by executing:
bundle add ractor-pool
If bundler is not being used to manage dependencies, install the gem by executing:
gem install ractor-pool
Usage
Calculating Fibonacci numbers in parallel:
fib_worker = proc do |index|
first, second = 0, 1
index.times { first, second = second, first + second }
[index, first]
end
results = []
pool = RactorPool.new(size: 2, worker: fib_worker) { |result| results << result }
25.times { |index| pool << index }
pool.shutdown
results.sort_by! { |index, _value| index }
results.each { |index, value| puts "fib(#{index}) = #{value}" }
#=> fib(0) = 0
#=> fib(1) = 1
#=> fib(2) = 1
#=> fib(3) = 2
#=> ...
#=> fib(21) = 10946
#=> fib(22) = 17711
#=> fib(23) = 28657
#=> fib(24) = 46368
Development
After checking out the repo, run bin/setup to install dependencies. Then, run bundle exec rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
Contributing
Bug reports and pull requests are welcome on GitHub at github.com/joshuay03/ractor-pool. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the RactorPool project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
Acknowledgements
Thanks to Gleb Sinyavskiy (zhulik) for graciously transferring ownership of the ractor_pool gem name, enabling this gem to be published as ractor-pool.