Actors for Ruby: The Omnibus Concurrency Library 0.1


#1

With all the recent interest in Erlang, I’ve decided to bump up the
release schedule of my Omnibus Concurrency Library. It provides a
Ruby-esque implementation of actors, as well as a few other concurrency
approaches (data-parallel programming and futures, to begin with).

This first release is a very rough cut, featuring an MRI port of the
Actors implementation I wrote for the Rubinius core. I’m afraid there’s
no documentation yet, so I’ll give some brief examples:

require ‘concurrent/actors’
include Concurrent::Actors

RequestGreeting = Struct.new :reply_to
Greeting = Struct.new :value

spawning a new actor

oracle = Actor.spawn do
greeting = “Hello”
loop do
# guarded receive (does case-like matching via #===)
Actor.receive do |f|
f.when Greeting do |m|
greeting = m.value.dup.freeze
end
# callback part 1
f.when RequestGreeting do |m|
m.reply_to << Greeting[greeting]
end
end
end
end

sending a message to an actor

oracle << Greeting[“Howdy”]

getting a reference to the current actor

current = Actor.current

callback part 2

oracle << RequestGreeting[current]
Actor.receive do |f|
f.when Greeting do |m|
puts “#{ m.value }, Bill!”
end
end

Linked processes, distributed actors, and possible integration with
Erlang/Erlectricity are planned for the future.

The Omnibus Concurrency Library is available via RubyGems as the
“concurrent” gem, and downloads are also available from the RubyForge
download page:

http://rubyforge.org/frs/?group_id=3690

-mental