Concurrent Ruby Build Status Coverage Status Dependency Status

Modern concurrency tools including agents, futures, promises, thread pools, supervisors, and more. Inspired by Erlang, Clojure, Scala, Go, Java, JavaScript, and classic concurrency patterns.

Introduction

The old-school "lock and synchronize" approach to concurrency is dead. The future of concurrency is asynchronous. Send out a bunch of independent actors to do your bidding and process the results when you are ready. Many modern programming languages (like Erlang, Clojure, Scala, Haskell, F#, C#, Java...) provide asynchronous concurrency mechanisms within their standard libraries, the runtime environment, or the language iteself. This library implements a few of the most interesting and useful of those variations.

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

The project is hosted on the following sites:

Conference Presentations

I've given several conference presentations on concurrent programming with this gem. Check them out:

Goals

  • 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
  • Keep everything small
  • Be as fast as reasonably possible

Features (and Documentation)

Several features from Erlang, Go, Clojure, Java, and JavaScript have been implemented thus far:

Is it any good?

Yes

Supported Ruby versions

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.

Install

gem install concurrent-ruby

or add the following line to Gemfile:

gem 'concurrent-ruby'

and run bundle install from your shell.

Once you've installed the gem you must require it in your project:

require 'concurrent'

Examples

For complete examples, see the specific documentation for each abstraction. The examples below are just basic usage.

Goroutine (Go)

Full documentation: Goroutine

require 'concurrent'

go('foo'){|echo| sleep(0.1); print "#{echo}\n"; sleep(0.1); print "Boom!\n" }
sleep(0.5)

#=> foo
#=> Boom!

Agent (Clojure)

Full documentation: Agent

require 'concurrent'

score = Concurrent::Agent.new(10)
score.value #=> 10

score << proc{|current| current + 100 }
sleep(0.1)
score.value #=> 110

Future (Clojure)

Full documentation: Future

require 'concurrent'

count = Concurrent::Future.new{ sleep(1); 10 }
count.state #=> :pending
# do stuff...
count.value #=> 10 (after blocking)

Promise (JavaScript)

Full documentation: Promise

require 'concurrent'

p = Concurrent::Promise.new("Jerry", "D'Antonio"){|a, b| "#{a} #{b}" }.
    then{|result| "Hello #{result}." }.
    rescue(StandardError){|ex| puts "Boom!" }.
    then{|result| "#{result} Would you like to play a game?"}
sleep(1)
p.value #=> "Hello Jerry D'Antonio. Would you like to play a game?" 

Thread Pools (Java)

Full documentation: Thread Pools

require 'concurrent'

pool = Concurrent::FixedThreadPool.new(2)
pool.size #=> 2

pool.post{ sleep(0.5); print "Boom!\n" }
pool.size #=> 2

sleep(1)
#=> Boom!

pool = Concurrent::CachedThreadPool.new
pool.size #=> 0

pool << proc{ sleep(0.5); print "Boom!\n" }
pool.size #=> 1

sleep(1)
#=> Boom!

TimerTask (Java)

Full documentation: TimerTask

require 'concurrent'

ec = Concurrent::TimerTask.run{ puts 'Boom!' }

ec.execution_interval #=> 60 == Concurrent::TimerTask::EXECUTION_INTERVAL
ec.timeout_interval   #=> 30 == Concurrent::TimerTask::TIMEOUT_INTERVAL
ec.status             #=> "sleep"

# wait 60 seconds...
#=> 'Boom!'

ec.kill #=> true

ScheduledTask (Java)

Full documentation: ScheduledTask

TBD

Actor (Scala)

Full documentation: Actor

class FinanceActor < Concurrent::Actor
  def act(query)
    finance = Finance.new(query)
    print "[#{Time.now}] RECEIVED '#{query}' to #{self} returned #{finance.update.suggested_symbols}\n\n"
  end
end

financial, pool = FinanceActor.pool(5)

pool << 'YAHOO'
pool << 'Micosoft'
pool << 'google'

Supervisor (Erlang)

Full documentation: Supervisor

pong = Pong.new
ping = Ping.new(10000, pong)
pong.ping = ping

task = Concurrent::TimerTask.new{ print "Boom!\n" }

boss = Concurrent::Supervisor.new
boss.add_worker(ping)
boss.add_worker(pong)
boss.add_worker(task)

boss.run!

ping << :pong

Todo

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. 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.

License

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.