Your SlideShare is downloading. ×
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Ruby goes to Hollywood
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Ruby goes to Hollywood

1,396

Published on

Description of the actor model. Which libraries implement it in Ruby? What are the successful actor libraries in other

Description of the actor model. Which libraries implement it in Ruby? What are the successful actor libraries in other

Published in: Business, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,396
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Ruby goes toRuby, @elise_huard Rubykaigi 2011
  • 2. Context
  • 3. “Concurrency is hard”
  • 4. “Shared and Mutable State”
  • 5. “Why Bother?”
  • 6. Actors
  • 7. “Actor Model” (1973) (1973)
  • 8. “No Global State”” ” “Actors” ” ”“Partial Order of Execution” ” ”
  • 9. MutableMutable
  • 10. “Sleeping Barber”
  • 11. The universe customer X wake up customers? Barbersleeping/cutting customer X BarberShop customers waiting no
  • 12. Ruby
  • 13. “Rubinius Actors” @mentalguy
  • 14. “Revactor” @bascule
  • 15. “Celluloid”
  • 16. 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)) Text accident = rand(2) if accident puts " finished cutting #{name}" else puts " *** whoops, chopped #{name}s head off ***" end shop.customer_leaves! end(...)end
  • 17. 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) Text 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 endend
  • 18. shop = Shop.new[jack, john, henry, tom, bob].eachdo |customer| Text shop.new_customer!(customer)endshop.report
  • 19. “CRuby”
  • 20. Foreign lands
  • 21. “Erlang”
  • 22. %% 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.
  • 23. %% barber %%barber(ShopPid) -> ShopPid ! barbercheck, receive wakeup -> barber(ShopPid); {customer, Customer} -> cut(ShopPid, Customer), barber(ShopPid) end. Textcut(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]).
  • 24. scenario() -> ShopPid = start_shop(), % send customers to the shop ShopPid ! john, ShopPid ! joe,Text timer:sleep(20000), ShopPid ! kevin, ShopPid ! richard, main_end.
  • 25. http://vimeo.com/26560141 Text
  • 26. “Elixir”
  • 27. 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 Text 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 (...)end
  • 28. 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) Text end end def cut_hair(shop_pid, customer) IO.puts "cutting customer #{customer}" shop_pid <- {cut, customer} IO.puts "finished cutting customer #{customer}" endend
  • 29. pid = Process.spawn -> Shop.new.loop([])IO.puts pidpid <- {customer, "jack"} Textpid <- {customer, "bob"}
  • 30. “Scala”
  • 31. class BarberShop extends Actor { var barber: ActorRef = null var customersInChairs = List[Customer]() override def preStart() { barber = Actor.actorOf(new Barber(self)).start() } def receive = { case Customer(name) => if (customersInChairs.length >= 3) { Text println("sorry, no room for " + name) } else { customersInChairs = customersInChairs :+ Customer(name) barber ! WakeUp } case Cut(name) => customersInChairs = customersInChairs.tail case BarberCheck => // send first customer barber ! customersInChairs.head }}
  • 32. class Barber(val shop: ActorRef) extends Actor { def cut(name: String) = { shop ! Cut(name) } def receive = { case WakeUp => shop ! BarberCheck Text case Customer(name) => cut(name) shop ! BarberCheck }}
  • 33. package sleepingBarberimport akka.actor.{Actor, ActorRef, ActorRegistry}import Actor._import akka.actor.Actor.actorOf/* messages classes */sealed trait BarberMessagecase class Customer(name: String) extends BarberMessagecase class Cut(name: String) extends BarberMessagecase class BarberCheck() extends BarberMessage Textcase class NoCustomers() extends BarberMessagecase class WakeUp() extends BarberMessageobject SleepingBarber { def main(args: Array[String]) : Unit = { val barberShop = Actor.actorOf[BarberShop].start() barberShop ! Customer("John") barberShop ! Customer("Jack") barberShop ! Customer("Arthur") Actor.registry.shutdownAll }}
  • 34. “JRuby”
  • 35. Caveat
  • 36. “easy to understand” “ ” “real encapsulation” ” ” “highly decentralized”” ”
  • 37. “clear separation of parallel components” “ ” “livelocks - deadlocks still possible”“ - ”
  • 38. “Concurrency Primitives”
  • 39. Epilogue
  • 40. “Time to control shared state?” shared state
  • 41. “Better Concurrency Primitives in stdlib?”
  • 42. ! Speaker Pictures Sherlock Jr (1924)@elise_huard Safety Last (1923) A Woman of Affairs (1928) Mysteries of a Barbershop (1923)Translation The Doll (1919) Intolerance (1926) Metropolis (1927) He Who Gets Slapped (1924) Conference Laughing Gravy (1931) DocumentationRuby Kaigi 2011 http://www.delicious.com/elisehuard/concurrency Sponsor Forward
  • 43. The End @ @@elise_huard Translation:

×