Upgrade to Pro — share decks privately, control downloads, hide ads and more …

NWRUG - Celluloid

NWRUG - Celluloid

Celluloid talk for NWRUG

Adam Holt

June 20, 2012
Tweet

More Decks by Adam Holt

Other Decks in Programming

Transcript

  1. “Celluloid is a concurrent object oriented programming framework for Ruby

    which lets you build multithreaded programs out of concurrent objects just as easily as you build sequential programs out of regular objects”
  2. class Sheen include Celluloid def initialize(name) @name = name end

    def set_status(status) @status = status end def report "#{@name} is #{@status}" end end >> charlie = Sheen.new "Charlie Sheen" => #<Celluloid::Actor(Sheen:0x00000100a312d0) @name="Charlie Sheen"> >> charlie.set_status "winning!" => "winning!" >> charlie.report => "Charlie Sheen is winning!" >> charlie.set_status! "asynchronously winning!" => nil >> charlie.report => "Charlie Sheen is asynchronously winning!" Actors
  3. >> james = JamesDean.new => #<Celluloid::Actor(JamesDean:0x80c27ce0)> >> Celluloid::Actor[:james] = james

    => #<Celluloid::Actor(JamesDean:0x80c27ce0)> >> Celluloid::Actor[:james] => #<Celluloid::Actor(JamesDean:0x80c27ce0)> Actor Registry class MyActor include Celluloid def initialize(name) @name = name @other_actor = Actor[:other_actor] end end
  4. >> future = charlie.future :report => #<Celluloid::Future:0x000001009759b8> >> future.value =>

    "Charlie Sheen is winning!" Futures >> future = Celluloid::Future.new { 2 + 2 } => #<Celluloid::Future:0x000001008425f0> >> future.value => 4
  5. Linking class JamesDean include Celluloid class CarInMyLaneError < StandardError; end

    def drive_little_bastard raise CarInMyLaneError, "that guy's gotta stop. he'll see us" end end >> james = JamesDean.new => #<Celluloid::Actor(JamesDean:0x1068)> >> james.drive_little_bastard! => nil >> james => #<Celluloid::Actor(JamesDean:0x1068) dead>
  6. Linking class ElizabethTaylor include Celluloid trap_exit :actor_died def actor_died(actor, reason)

    p "Oh no! #{actor.inspect} has died because of a #{reason.class}" end end >> james = JamesDean.new => #<Celluloid::Actor(JamesDean:0x11b8)> >> elizabeth = ElizabethTaylor.new => #<Celluloid::Actor(ElizabethTaylor:0x11f0)> >> elizabeth.link james => #<Celluloid::Actor(JamesDean:0x11b8)> >> james.drive_little_bastard! => nil "Oh no! #<Celluloid::Actor(JamesDean:0x11b8) dead> has died because of a JamesDean::CarInMyLaneError"
  7. Linking class PorscheSpider include Celluloid class CarInMyLaneError < StandardError; end

    def drive_on_route_466 raise CarInMyLaneError, "head on collision :(" end end class JamesDean include Celluloid def initialize @little_bastard = PorscheSpider.new_link end def drive_little_bastard @little_bastard.drive_on_route_466 end end
  8. Linking >> james = JamesDean.new => #<Celluloid::Actor(JamesDean:0x1108) @little_bastard=#<Celluloid::Actor(PorscheSpider:0x10ec)>> >> elizabeth

    = ElizabethTaylor.new => #<Celluloid::Actor(ElizabethTaylor:0x1144)> >> elizabeth.link james => #<Celluloid::Actor(JamesDean:0x1108) @little_bastard=#<Celluloid::Actor(PorscheSpider:0x10ec)>> >> james.drive_little_bastard! => nil "Oh no! #<Celluloid::Actor(JamesDean:0x1108) dead> has died because of a PorscheSpider::CarInMyLaneError"
  9. Supervisors >> supervisor = Sheen.supervise "Charlie Sheen" => #<Celluloid::Supervisor(Sheen) "Charlie

    Sheen"> >> charlie = supervisor.actor => #<Celluloid::Actor(Sheen:0x00000100a312d0)> >> Sheen.supervise_as :charlie, "Charlie Sheen" => #<Celluloid::Supervisor(Sheen) "Charlie Sheen"> >> charlie = Celluloid::Actor[:charlie] => #<Celluloid::Actor(Sheen:0x00000100a312d0)> class MyGroup < Celluloid::SupervisionGroup supervise MyActor, :as => :my_actor supervise AnotherActor, :as => :another_actor, :args => [{:start_working_right_now => true}] end MyGroup.run # or MyGroup.run!
  10. Other Features pool = MyWorker.pool pool = MyWorker.pool(size: 9000, args:

    [:foo, :bar, :baz]) Pools class TimerExample include Celluloid attr_reader :fired, :timer def initialize @fired = false @timer = after(3) { puts "Timer fired!"; @fired = true } end end Timers And More...