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

Celluloid & DCell

Celluloid & DCell

Patrick Van Stee

July 11, 2012
Tweet

More Decks by Patrick Van Stee

Other Decks in Programming

Transcript

  1. Celluloid
    DCell
    &

    View full-size slide

  2. @vanstee
    github.com/vanstee
    Patrick Van Stee
    highgroove.com

    View full-size slide

  3. @tarcieri
    Revactor
    Reia
    nio4r
    Cryptosphere
    cool.io

    View full-size slide

  4. brew install zeromq
    gem install dcell
    Setup

    View full-size slide

  5. Celluloid
    General purpose concurrency
    framework for Ruby built on the
    Actor Model

    View full-size slide

  6. class Counter
    attr_reader :count
    def initialize
    @count = 0
    end
    def increment
    @count += 1
    end
    end

    View full-size slide

  7. class Counter
    attr_reader :count
    def initialize
    @count = 0
    @mutex = Mutex.new
    end
    def increment
    @mutex.synchronize do
    @count += 1
    end
    end
    end

    View full-size slide

  8. Threads
    are
    hard!

    View full-size slide

  9. class Counter
    include Celluloid
    attr_reader :count
    def initialize
    @count = 0
    end
    def increment
    @count += 1
    end
    end

    View full-size slide

  10. •No shared state
    •Communicate with
    messages
    •Process messages
    sequentially

    View full-size slide

  11. class Counter
    include Celluloid
    def increment(count, actor)
    return count if count < 10000
    actor.increment(
    count + 1,
    Actor.current
    )
    end
    end

    View full-size slide

  12. &
    Asynchronous
    Method Calls
    Futures

    View full-size slide

  13. counter = Counter.new
    # returns immediately
    counter.increment!
    puts counter.count
    # returns immediately
    future = counter.future :increment
    puts future.value

    View full-size slide

  14. module Enumerable
    def map(&block)
    futures = map do |item|
    Celluloid::Future.new(
    item,
    &block
    )
    end
    futures.map(&:value)
    end
    end

    View full-size slide

  15. • The GIL in MRI does
    not allow parallelism
    • Threads and Fibers
    are expensive
    • Concurrency is still a
    hard problem
    [not really]

    View full-size slide

  16. Celluloid
    General purpose concurrency
    framework for Ruby built on the
    Actor Model
    DCell
    Distributed Celluloid over 0MQ

    View full-size slide

  17. “I thought of objects being like
    biological cells or individual
    computers on a network, only
    able to communicate with
    messages. Alan Kay

    View full-size slide

  18. • Exposes Actors on the
    Network
    • Send messages as you
    normally would

    View full-size slide

  19. DRb DCell
    Threads Actors

    View full-size slide

  20. Also
    Checkout
    Sidekiq

    View full-size slide

  21. github.com
    celluloid/celluloid
    celluloid/dcell

    View full-size slide