Modern concurrency tools for Ruby. Inspired by Erlang, Clojure, Scala, Haskell, F#, C#, Java, and classic concurrency patterns.
The design goals of this gem are:
- Stay true to the spirit of the languages providing inspiration
- But implement in a way that makes sense for Ruby
- Keep the semantics as idiomatic Ruby as possible
- Support features that make sense in Ruby
- Exclude features that don't make sense in Ruby
- Be small, lean, and loosely coupled
- Clojure-inspired Agent
- Clojure-inspired Future
- Scala-inspired Actor
- JavaScript-inspired Promise
- Java-inspired Thread Pools
- Old school events from back in my Visual C++ days
- Repeated task execution with Java-inspired TimerTask service
- Scheduled task execution with Java-inspired ScheduledTask service
- Erlang-inspired Supervisor for managing long-running threads
- Actor variant called
Channel
loosely based on the MailboxProcessor agent in F#
Beginning with v0.4.0 this gem will strictly adhere to the rules of semantic versioning.
MRI 1.9.2, 1.9.3, 2.0, 2.1, and JRuby (1.9 mode). This library is pure Ruby and has no gem dependencies. It should be fully compatible with any Ruby interpreter that is 1.9.x compliant. I simply don't know enough about Rubinius or the others to fully support them. I can promise good karma and attribution on this page to anyone wishing to take responsibility for verifying compaitibility with any Ruby other than MRI.
Many more code examples can be found in the documentation for each class (linked above). This one simple example shows some of the power of this gem.
require 'concurrent'
require 'faker'
class EchoActor < Concurrent::Actor
def act(*message)
puts "#{message} handled by #{self}"
end
end
mailbox, pool = EchoActor.pool(5)
timer_proc = proc do
mailbox.post(Faker::Company.bs)
end
t1 = Concurrent::TimerTask.new(execution_interval: rand(5)+1, &timer_proc)
t2 = Concurrent::TimerTask.new(execution_interval: rand(5)+1, &timer_proc)
overlord = Concurrent::Supervisor.new
overlord.add_worker(t1)
overlord.add_worker(t2)
pool.each{|actor| overlord.add_worker(actor)}
overlord.run!
#=> ["mesh proactive platforms"] handled by #<EchoActor:0x007fa5ac18bdf8>
#=> ["maximize sticky portals"] handled by #<EchoActor:0x007fa5ac18bdd0>
#=> ["morph bleeding-edge markets"] handled by #<EchoActor:0x007fa5ac18bd80>
#=> ["engage clicks-and-mortar interfaces"] handled by #<EchoActor:0x007fa5ac18bd58>
#=> ["monetize transparent infrastructures"] handled by #<EchoActor:0x007fa5ac18bd30>
#=> ["morph sexy e-tailers"] handled by #<EchoActor:0x007fa5ac18bdf8>
#=> ["exploit dot-com models"] handled by #<EchoActor:0x007fa5ac18bdd0>
#=> ["incentivize virtual deliverables"] handled by #<EchoActor:0x007fa5ac18bd80>
#=> ["enhance B2B models"] handled by #<EchoActor:0x007fa5ac18bd58>
#=> ["envisioneer real-time architectures"] handled by #<EchoActor:0x007fa5ac18bd30>
overlord.stop
Remember, there is no silver bullet in concurrent programming. Concurrency is hard. These tools will help ease the burden, but at the end of the day it is essential that you know what you are doing.
- Decouple business logic from concurrency logic
- Test business logic separate from concurrency logic
- Keep the intersection of business logic and concurrency and small as possible
- Don't share mutable data unless absolutely necessary
- Protect shared data as much as possible (prefer immutability)
- Don't mix Ruby's concurrency primitives with asynchronous concurrency libraries
I've given several conference presentations on concurrent programming with this gem. Check them out:
- "Advanced Concurrent Programming in Ruby" at RubyConf 2013 used this version of the presentation and is available for viewing on Confreaks
- "Advanced Multithreading in Ruby" at Cascadia Ruby 2013 used this version of the presentation and is available for viewing on Confreaks
- Cleveland Ruby Brigade meetup in December of 2013 used this version of the presentation
- I'll be giving "Advanced Concurrent Programming in Ruby" at CodeMash 2014
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Concurrent Ruby is Copyright © 2013 Jerry D'Antonio. It is free software and may be redistributed under the terms specified in the LICENSE file.
Released under the MIT license.
http://www.opensource.org/licenses/mit-license.php
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.