Giter Club home page Giter Club logo

actor's Introduction

Actor

Build Status

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.

  1. Actor::Base overrides the including class's :new method and renames it to :__actor_new. This sets up the possibility of naming conflicts.
  2. Actor::Base overrides :send, making it impossible to use :send without concurrent execution and callbacks.
  3. 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).
  4. 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

  1. Using a thread pool would be nice.
  2. Benchmarks comparing MRI 2.1.2 vs. Rubinisu 2.2.6.
  3. Benchmarks comparing of this gem vs. Celluloid
  4. Add a way of using actors over a network via RPC

Contributing

  1. Fork it ( https://github.com/maxgale/actor/fork )
  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 a new Pull Request

actor's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.