Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up
for free
Celluloid & DCell
Patrick Van Stee
July 11, 2012
Programming
4
260
Celluloid & DCell
Patrick Van Stee
July 11, 2012
Tweet
Share
More Decks by Patrick Van Stee
See All by Patrick Van Stee
vanstee
116
4.8k
vanstee
3
540
vanstee
8
500
vanstee
11
350
vanstee
22
1.8k
vanstee
4
360
vanstee
1
120
vanstee
10
470
Other Decks in Programming
See All in Programming
hirotokirimaru
1
410
taoshotaro
1
360
momofff
0
160
line_developers_tw
0
460
anchorcable
1
120
showwin
0
120
malvinstn
1
630
sullis
0
120
xrdnk
0
130
komagata
1
1.8k
siketyan
1
110
nbkouhou
1
1.1k
Featured
See All Featured
swwweet
206
6.8k
erikaheidi
13
4.2k
rocio
155
11k
philnash
8
490
danielanewman
200
19k
zakiwarfel
88
3.3k
cherdarchuk
71
260k
yeseniaperezcruz
302
31k
mza
80
4.1k
gr2m
83
11k
marcelosomers
220
15k
dotmariusz
94
5.1k
Transcript
Celluloid DCell &
@vanstee github.com/vanstee Patrick Van Stee highgroove.com
@tarcieri Revactor Reia nio4r Cryptosphere cool.io
brew install zeromq gem install dcell Setup
Celluloid General purpose concurrency framework for Ruby built on the
Actor Model
class Counter attr_reader :count def initialize @count = 0 end
def increment @count += 1 end end
class Counter attr_reader :count def initialize @count = 0 @mutex
= Mutex.new end def increment @mutex.synchronize do @count += 1 end end end
Threads are hard!
class Counter include Celluloid attr_reader :count def initialize @count =
0 end def increment @count += 1 end end
Actor Model
•No shared state •Communicate with messages •Process messages sequentially
class Counter include Celluloid def increment(count, actor) return count if
count < 10000 actor.increment( count + 1, Actor.current ) end end
& Asynchronous Method Calls Futures
counter = Counter.new # returns immediately counter.increment! puts counter.count #
returns immediately future = counter.future :increment puts future.value
module Enumerable def map(&block) futures = map do |item| Celluloid::Future.new(
item, &block ) end futures.map(&:value) end end
Caveats
• The GIL in MRI does not allow parallelism •
Threads and Fibers are expensive • Concurrency is still a hard problem [not really]
DCell
Celluloid General purpose concurrency framework for Ruby built on the
Actor Model DCell Distributed Celluloid over 0MQ
“I thought of objects being like biological cells or individual
computers on a network, only able to communicate with messages. Alan Kay
• Exposes Actors on the Network • Send messages as
you normally would
DRb DCell Threads Actors
Demo
Also Checkout Sidekiq
github.com celluloid/celluloid celluloid/dcell
Hack Night