Slide 1

Slide 1 text

Ruby goes to @elise_huard FrozenRails 2011

Slide 2

Slide 2 text

Context

Slide 3

Slide 3 text

Concurrency is Hard

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Shared and Mutable State

Slide 6

Slide 6 text

Why Bother?

Slide 7

Slide 7 text

Actors

Slide 8

Slide 8 text

Actor Model (1973)

Slide 9

Slide 9 text

No Global State Actors Partial Order of Execution

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Objects were meant to be Actors

Slide 12

Slide 12 text

spawn send receive

Slide 13

Slide 13 text

Sleeping Barber

Slide 14

Slide 14 text

Barber BarberShop The universe customer X customers waiting wake up sleeping/cutting customers? customer X no

Slide 15

Slide 15 text

Ruby

Slide 16

Slide 16 text

Rubinius Actors @mentalguy

Slide 17

Slide 17 text

Celluloid @bascule

Slide 18

Slide 18 text

Text class Barber include Celluloid::Actor attr_accessor :status def initialize(shop) @shop = shop end def cut_customer(name) puts " cutting customer #{name}" sleep(rand(5)) accident = rand(2) if accident puts " finished cutting #{name}" else puts " *** whoops, chopped #{name}'s head off ***" end shop.customer_leaves! end (...) end

Slide 19

Slide 19 text

Text class Shop include Celluloid::Actor def initialize @barber = Barber.new(self) @chairs = [] puts "lights on ..." end def waiting_line @chairs.size end def new_customer(name) puts " customer arriving: #{name}" if waiting_line > 3 puts "*** sorry, #{name}, no room ***" else @chairs << name @barber.cut_customer!(name) end end def customer_leaves @chairs.shift end end

Slide 20

Slide 20 text

Text shop = Shop.new ['jack', 'john', 'henry', 'tom', 'bob'].each do |customer| shop.new_customer!(customer) end shop.report

Slide 21

Slide 21 text

CRuby

Slide 22

Slide 22 text

Cheat on Ruby

Slide 23

Slide 23 text

Erlang

Slide 24

Slide 24 text

%% shop %% barber_shop() -> io:format("lights on ... ~n"), ShopPid = self(), BarberPid = spawn(fun() -> barber(ShopPid) end), barber_shop_in_business(BarberPid, []). barber_shop_in_business(BarberPid, CustomersInChairs) -> receive {cut, Customer} -> % pop customer from list when hair is being cut remove_customer(BarberPid, CustomersInChairs, Customer); barbercheck -> respond_to_barber(BarberPid, CustomersInChairs); Customer -> io:format("~p arrives~n",[Customer]), % should check the number of customers waiting first % if all full: customer turned away add_customer_if_available(BarberPid, CustomersInChairs, Customer) end.

Slide 25

Slide 25 text

Text %% barber %% barber(ShopPid) -> ShopPid ! barbercheck, receive wakeup -> barber(ShopPid); {customer, Customer} -> cut(ShopPid, Customer), barber(ShopPid) end. cut(ShopPid, Name) -> io:format("cutting ~p~n", [Name]), % take customer -> can be removed from waiting chairs ShopPid ! {cut, Name}, timer:sleep(random:uniform(10000)), io:format("finished cutting: ~p~n", [Name]).

Slide 26

Slide 26 text

Text scenario() -> ShopPid = start_shop(), % send customers to the shop ShopPid ! 'john', ShopPid ! 'joe', timer:sleep(20000), ShopPid ! 'kevin', ShopPid ! 'richard', main_end.

Slide 27

Slide 27 text

Text

Slide 28

Slide 28 text

Elixir

Slide 29

Slide 29 text

Text object Shop def initialize shop_pid = Process.current barber_pid = Process.spawn -> Barber.loop(shop_pid) @('barber_pid: barber_pid, 'shop_pid: shop_pid) end def loop(customers_in_chairs) IO.puts "shop loop #{customers_in_chairs}" receive match {'cut, customer} if customers_in_chairs.include?(customer) loop(customers_in_chairs.delete(customer)) else loop(customers_in_chairs) end match 'barbercheck IO.puts "barber checking ..." respond_to_barber(customers_in_chairs) match {'customer, customer} IO.puts("new customer #{customer}") add_customer(customers_in_chairs, customer) end end (...)

Slide 30

Slide 30 text

Text module Barber def loop(shop_pid) shop_pid <- 'barbercheck receive match 'wakeup IO.puts "wakeup" loop(shop_pid) match {'customer, customer} cut_hair(shop_pid, customer) loop(shop_pid) end end def cut_hair(shop_pid, customer) IO.puts "cutting customer #{customer}" shop_pid <- {'cut, customer} IO.puts "finished cutting customer #{customer}" end end

Slide 31

Slide 31 text

Text pid = Process.spawn -> Shop.new.loop([]) IO.puts pid pid <- {'customer, "jack"} pid <- {'customer, "bob"}

Slide 32

Slide 32 text

Scala

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

JRuby

Slide 35

Slide 35 text

Text class Conductor < Akka::UntypedActor def self.spawn actor = Akka::Actors.actor_of { new } actor.set_dispatcher(Akka::Dispatchers.newThreadBasedDispatcher(actor)) actor.start end def onReceive(msg) case msg when "go" puts 'play score' play_guitar play_drum when "done drumming" play_end_drum when "done guitar" play_guitar else puts "msg: #{msg}" end end def play_end_drum puts "play end drum" end_drum = EndDrum.spawn end_drum.send_one_way("play", context) end (...) end

Slide 36

Slide 36 text

Text class Musician < Akka::UntypedActor include Music def self.spawn actor = Akka::Actors.actor_of { new } actor.set_dispatcher(Akka::Dispatchers.newThreadBasedDispatche r(actor)) actor.start end def onReceive(msg) case msg when "play" @conductor = context.sender.get @conductor = Akka::Actors.registry.actor_for(@conductor.uuid).get play(@conductor) else puts "msg: #{msg}" end end end

Slide 37

Slide 37 text

Text class Guitar < Musician def play(conductor) play_mp3("Guitar_Loop_Riff.mp3") conductor.send_one_way("done guitar") end end

Slide 38

Slide 38 text

Text http://vimeo.com/29528327

Slide 39

Slide 39 text

Caveat

Slide 40

Slide 40 text

easy to understand real encapsulation highly decentralized

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

only when parallel components livelocks - deadlocks still possible different programming style

Slide 43

Slide 43 text

Concurrency Primitives

Slide 44

Slide 44 text

Epilogue

Slide 45

Slide 45 text

Time to control shared state?

Slide 46

Slide 46 text

Better Concurrency Primitives in stdlib?

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

Kiitos Kuunteluun! Speaker @elise_huard Conference Frozen Rails 2011 Sponsor Forward Pictures By The Waterfront Vertigo Casablanca Tarzan Wizard of Oz Some like it hot King Kong Citizen Kane Documentation http://www.delicious.com/elisehuard/concurrency