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

A Crash Course on Celluloid (GoGaRuCo 2012)

tarcieri
September 15, 2012

A Crash Course on Celluloid (GoGaRuCo 2012)

A quick introduction to the basic features of Celluloid

tarcieri

September 15, 2012
Tweet

More Decks by tarcieri

Other Decks in Programming

Transcript

  1. Sidekiq What if 1 Sidekiq process could do the work

    of 20 Resque processes? http://mperham.github.com/sidekiq/ Saturday, September 15, 12
  2. “I thought of objects being like biological cells and/or individual

    computers on a network, only able to communicate with messages” - Alan Kay, creator of Smalltalk, on the meaning of "object oriented programming" Saturday, September 15, 12
  3. Actor Model •Actors are computational entities that can receive messages

    •Each actor has a unique address •If you know an actor’s address, you can send it messages •Actors can create new actors Saturday, September 15, 12
  4. require 'celluloid' class Launcher include Celluloid def launch 3.downto(1) do

    |count| puts "#{count}..." sleep 1 end puts "BLASTOFF!" end end Saturday, September 15, 12
  5. require 'celluloid' class Launcher include Celluloid def launch 3.downto(1) do

    |count| puts "#{count}..." sleep 1 end puts "BLASTOFF!" end end Saturday, September 15, 12
  6. &DOOHU &HOOXORLG $FWRU3UR[\ &$// 5HFHLYHU &HOOXORLG 0DLOER[ 5(63216( &HOOXORLG 0DLOER[

    &HOOXORLG$FWRU &HOOXORLG&DOO &HOOXORLG5HVSRQVH Synchronous Calls Saturday, September 15, 12
  7. class FibonacciWorker include Celluloid def fib(n) n < 2 ?

    n : fib(n-1) + fib(n-2) end end Saturday, September 15, 12
  8. >> worker = FibonacciWorker.new => #<Celluloid::Actor(FibonacciWorker:0x3ffdcaed65cc)> >> future = worker.future.fib(40)

    => #<Celluloid::Future:0x007ffb9201f0a0> Returns immediately Saturday, September 15, 12
  9. >> worker = FibonacciWorker.new => #<Celluloid::Actor(FibonacciWorker:0x3ffdcaed65cc)> >> future = worker.future.fib(40)

    => #<Celluloid::Future:0x007ffb9201f0a0> >> future.value Saturday, September 15, 12
  10. >> worker = FibonacciWorker.new => #<Celluloid::Actor(FibonacciWorker:0x3ffdcaed65cc)> >> future = worker.future.fib(40)

    => #<Celluloid::Future:0x007ffb9201f0a0> >> future.value Blocks until complete Saturday, September 15, 12
  11. >> worker = FibonacciWorker.new => #<Celluloid::Actor(FibonacciWorker:0x3ffdcaed65cc)> >> future = worker.future.fib(40)

    => #<Celluloid::Future:0x007ffb9201f0a0> >> future.value => 102334155 Saturday, September 15, 12
  12. &DOOHU &HOOXORLG $FWRU3UR[\ 5HFHLYHU &HOOXORLG 0DLOER[ &HOOXORLG$FWRU &HOOXORLG&DOO &$// &HOOXORLG

    0DLOER[ )8785( &HOOXORLG )XWXUH &HOOXORLG5HVSRQVH 9$/8(" 9$/8( Futures Saturday, September 15, 12
  13. >> pool = FibonacciWorker.pool => #<Celluloid::Pool(FibonacciWorker:0x3ffdcaed65cc)> >> (32...40).map { |n|

    pool.future.fib(n) }.map(&:value) => [2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986] Saturday, September 15, 12
  14. Beware the GIL •YARV can do parallel I/O in threads

    •...but NOT parallel computation (e.g. fib) •JRuby and Rubinius will light up all your cores :) Saturday, September 15, 12