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
Rubygoes to
@elise_huard Rubykaigi 2011
Ruby,ハリウッドに行く
Contextコンテキスト
“Concurrency is hard”並行処理は難しい
“Shared and Mutable State”状態の共有と変更
“Why Bother?”だからなんだっていうの?
Actors アクターズ
“Actor Model” (1973)アクターモデル (1973)
“No Global State””グローバル状態がない”
“Actors””アクター”
“Partial Order of Execution””部分実行”
非同期Mutableメッセージ
アクター
独立したMutable状態
“Sleeping Barber”居眠り床屋問題
Barber BarberShop
The universe
customer X
customers waiting
wake up
sleeping/cutting
customers?customer X
no
理容師
眠る/理髪する
床屋
顧客が待っている
一般世界
Rubyルビー
“Rubinius Actors”@mentalguy
“Revactor”@bascule
“Celluloid”
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
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
Text
shop = Shop.new['jack', 'john', 'henry', 'tom', 'bob'].each do |customer| shop.new_customer!(customer)endshop.report
“CRuby”
Foreign lands他言語
“Erlang”
%% 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.
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]).
Text
scenario() -> ShopPid = start_shop(), % send customers to the shop ShopPid ! 'john', ShopPid ! 'joe', timer:sleep(20000), ShopPid ! 'kevin', ShopPid ! 'richard', main_end.
“Elixir”
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
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
Text
pid = Process.spawn -> Shop.new.loop([])IO.puts pidpid <- {'customer, "jack"}pid <- {'customer, "bob"}
“Scala”
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 }}
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 }}
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 }}
“JRuby”
Caveatはまりどころ
“easy to understand”“簡単に理解できる”
“real encapsulation””本物”
“highly decentralized””高度に分散化された”
“clear separation of parallel components”“境界が明らかな時だけ”
“livelocks - deadlocks still possible”“ライブロック-でもデッドロックは依然として起こりえる”
“Concurrency Primitives”並行性のためのプリミティブ
Epilogueエピローグ
“Time to control shared state?”shared stateをコントロール
するための時間
“Better Concurrency Primitives
in stdlib?”並行性のためのより良いプリミティブを
スタンダードライブラリーに
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
ご清聴ありがとうございました !
終わりThe End
@@@elise_huardTranslation: 井上 真