Actor
The actor gem provides a completely implicit implementation of the actor pattern. The goal of the library is to provide an easy way to implement fast, concurrent code without having to worry about race conditions or unexpected side-effects.
Installation
Add this line to your application's Gemfile:
gem 'actor'
And then execute:
$ bundle
Or install it yourself as:
$ gem install actor
Usage
Actors
require 'actor/base'
class MyActor
include Actor::Base
def example_method
'hi'
end
end
my_actor = MyActor.new
That's it! Now any interations with my_actor
will be executed concurrently. It's also worth noting that there are no
return values. Instead, code is executed via message passing. Instead, there are callbacks.
Callbacks
my_actor.before_action :example_method do
# Code here is executed before :example_method is executed by the actor
puts 'before'
end
my_actor.after_action :example_method do |result|
# Code here is executed after :example_method is executed by the actor
puts 'after'
puts result
end
my_actor.example_method
=> before
=> after
=> hi
Timers
Another useful feature is the timer. The timer is an object that periodically executes a block of code.
require 'actor/timer'
# Create a timer the executes every 1/30th of a second. Only executes twice.
Actor::Timer.new 0.033, 2 do
puts 'hi'
end
=> hi
=> hi
Passing in 0
as the number of iterations to the timer causes it to execute indefinitely. You can also pause, resume,
and wait for timers to finish execution.
my_timer = Timer.new 0.033, 0 do
# Do periodic work
end
my_timer.pause # Temporarily stop work
my_timer.resume # Resume the work
my_timer.wait # Since `iterations = 0`, this will block forever
Gotchas
Unfortunately, there are a few "gotchas" when using this gem.
-
Actor::Base
overrides the including class's:new
method and renames it to:__actor_new
. This sets up the possibility of naming conflicts. -
Actor::Base
overrides:send
, making it impossible to use:send
without concurrent execution and callbacks. - All actors are wrapped in a proxy class. This proxy forwards all methods to the
instance to be executed in a concurrent way. This means it is impossible to execute code normally when handling the
proxy. You can access the underlying instance by calling
:__proxy_target
on the proxy. Note: No callbacks are not triggered when accessing the instance directly (unless you use send). - The timer period is counted from the end of the last block to the start of the next block. This means that the timer firing is very approximate and definitely not designed for blocking code.
Todo
- Using a thread pool would be nice.
- Benchmarks comparing MRI 2.1.2 vs. Rubinisu 2.2.6.
- Benchmarks comparing of this gem vs. Celluloid
- Add a way of using actors over a network via RPC
Contributing
- Fork it ( https://github.com/maxgale/actor/fork )
- 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 a new Pull Request