48
Ruby goes to @elise_huard Rubykaigi 2011 Ruby, ハリウッドに行く

Ruby goes to Hollywood

  • Upload
    ehuard

  • View
    1.400

  • Download
    3

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Ruby goes to Hollywood

Rubygoes to

@elise_huard Rubykaigi 2011

Ruby,ハリウッドに行く

Page 2: Ruby goes to Hollywood

Contextコンテキスト

Page 3: Ruby goes to Hollywood

“Concurrency is hard”並行処理は難しい

Page 4: Ruby goes to Hollywood
Page 5: Ruby goes to Hollywood

“Shared and Mutable State”状態の共有と変更

Page 6: Ruby goes to Hollywood

“Why Bother?”だからなんだっていうの?

Page 7: Ruby goes to Hollywood

Actors アクターズ

Page 8: Ruby goes to Hollywood

“Actor Model” (1973)アクターモデル (1973)

Page 9: Ruby goes to Hollywood

“No Global State””グローバル状態がない”

“Actors””アクター”

“Partial Order of Execution””部分実行”

Page 10: Ruby goes to Hollywood

非同期Mutableメッセージ

アクター

独立したMutable状態

Page 11: Ruby goes to Hollywood

“Sleeping Barber”居眠り床屋問題

Page 12: Ruby goes to Hollywood

Barber BarberShop

The universe

customer X

customers waiting

wake up

sleeping/cutting

customers?customer X

no

理容師

眠る/理髪する

床屋

顧客が待っている

一般世界

Page 13: Ruby goes to Hollywood
Page 14: Ruby goes to Hollywood

Rubyルビー

Page 15: Ruby goes to Hollywood

“Rubinius Actors”@mentalguy

Page 16: Ruby goes to Hollywood

“Revactor”@bascule

Page 17: Ruby goes to Hollywood

“Celluloid”

Page 18: Ruby goes to Hollywood

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

Page 19: Ruby goes to Hollywood

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 endend

Page 20: Ruby goes to Hollywood

Text

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

Page 21: Ruby goes to Hollywood

“CRuby”

Page 22: Ruby goes to Hollywood

Foreign lands他言語

Page 23: Ruby goes to Hollywood

“Erlang”

Page 24: Ruby goes to Hollywood

%% 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.

Page 25: Ruby goes to Hollywood

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]).

Page 26: Ruby goes to Hollywood

Text

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

Page 27: Ruby goes to Hollywood

Text

http://vimeo.com/26560141

Page 28: Ruby goes to Hollywood

“Elixir”

Page 29: Ruby goes to Hollywood

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 (...)end

Page 30: Ruby goes to Hollywood

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}" endend

Page 31: Ruby goes to Hollywood

Text

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

Page 32: Ruby goes to Hollywood

“Scala”

Page 33: Ruby goes to Hollywood
Page 34: Ruby goes to Hollywood

Text

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) { 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 }}

Page 35: Ruby goes to Hollywood

Text

class Barber(val shop: ActorRef) extends Actor { def cut(name: String) = { shop ! Cut(name) } def receive = { case WakeUp => shop ! BarberCheck case Customer(name) => cut(name) shop ! BarberCheck }}

Page 36: Ruby goes to Hollywood

Text

package sleepingBarber

import 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 BarberMessagecase class NoCustomers() extends BarberMessagecase class WakeUp() extends BarberMessage

object 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 }}

Page 37: Ruby goes to Hollywood

“JRuby”

Page 38: Ruby goes to Hollywood

Caveatはまりどころ

Page 39: Ruby goes to Hollywood
Page 40: Ruby goes to Hollywood

“easy to understand”“簡単に理解できる”

“real encapsulation””本物”

“highly decentralized””高度に分散化された”

Page 41: Ruby goes to Hollywood
Page 42: Ruby goes to Hollywood

“clear separation of parallel components”“境界が明らかな時だけ”

“livelocks - deadlocks still possible”“ライブロック-でもデッドロックは依然として起こりえる”

Page 43: Ruby goes to Hollywood

“Concurrency Primitives”並行性のためのプリミティブ

Page 44: Ruby goes to Hollywood

Epilogueエピローグ

Page 45: Ruby goes to Hollywood

“Time to control shared state?”shared stateをコントロール

するための時間

Page 46: Ruby goes to Hollywood

“Better Concurrency Primitives

in stdlib?”並行性のためのより良いプリミティブを

スタンダードライブラリーに

Page 47: Ruby goes to Hollywood

Speakerスピーカー

@elise_huardTranslation井上 真

Conferenceカンファレンス

Ruby Kaigi 2011

Sponsorスポンサー

Forward

Pictures写真

Sherlock Jr (1924)Safety Last (1923)

A Woman of Affairs (1928)Mysteries of a Barbershop (1923)

The Doll (1919)Intolerance (1926)Metropolis (1927)

He Who Gets Slapped (1924)Laughing Gravy (1931)

Documentationドキュメンテーション

http://www.delicious.com/elisehuard/concurrency

終わりThe End

@@@elise_huardTranslation: 井上 真

The End

Pictures写真

Sherlock Jr (1924)Safety Last (1923)

A Woman of Affairs (1928)Mysteries of a Barbershop (1923)

The Doll (1919)Intolerance (1926)Metropolis (1927)

He Who Gets Slapped (1924)Laughing Gravy (1931)

Documentationドキュメンテーション

http://www.delicious.com/elisehuard/concurrency

ご清聴ありがとうございました !

Page 48: Ruby goes to Hollywood

終わりThe End

@@@elise_huardTranslation: 井上 真