アクターモデルの話

B9dd885e3ef94ac5e0bcfdf4800ba703?s=47 tmaeda
October 21, 2017

 アクターモデルの話

B9dd885e3ef94ac5e0bcfdf4800ba703?s=128

tmaeda

October 21, 2017
Tweet

Transcript

  1. ΞΫλʔϞσϧͷ࿩ 2017.10.21 RubySapporo.beam #3 ·͑ͩͱ΋͖

  2. ࣗݾ঺հ @tmaeda (ג)͑ʹ͠ςοΫ ࠷ۙ͸RailsͷཪͰಈ͘SidekiqͷδϣϒΛॻ͘࢓ࣄ͕ଟ͍Ͱ ͢ ElixirΛ·ͬͨΓษڧதͰ͢

  3. &-*9*3ͷίʔυྫ defmodule Greeter do def hello do receive do name

    -> IO.puts "Hello #{name}" end hello() end end greeter = spawn(Greeter, :hello, []) send(greeter, "World")
  4. ࠓ೔͓࿩͍ͨ͜͠͠ͱ ΞΫλʔϞσϧ͕ੜ·Εͨഎܠ ΞΫλʔϞσϧ͸ԿΛղܾ͢Δͷ͔ʁ ΞΫλʔϞσϧͱͲ͏෇͖߹͑͹͍͍ͷ͔ʁ

  5. ΞΫλʔϞσϧ Carl Hewitt 1973 https://en.wikipedia.org/wiki/Actor_model

  6. πΠολʔ΋͋Δˠhttps://twitter.com/carlhewitt

  7.  1977ͷHewittͷ࿦จ In recent years, there has been a shift

    from the centralized serial computing system to the distributed parallel computing network. (Laws for Communicating Parallel Processes)
 https://dspace.mit.edu/bitstream/handle/1721.1/41962/AI_WP_134A.pdf ڞײͰ͖·͢ʁ ͪΐͬͱ౰࣌ͷ༷ࢠΛௐ΂ͯΈ·͠ΐ͏
  8. ʰ·Μ͕͸͡Ίͯ෺ޠʱʢ·Μ͕͸͡Ίͯ΋ͷ͕ͨΓʣͱ͸ɺ1978೥5݄6೔͔Β1984೥3݄31೔·ͰTBSܥྻ ہͰ์ૹ͞ΕͨμοΫεΠϯλʔφγϣφϧͱTBSڞಉ੡࡞ͷࢠڙ޲͚ͷڭཆ൪૊Ͱ͋Δɻશ305ճɻ

  9. ࠷ॳͷίϯϐϡʔλ 1946 ENIAC

  10. ࠷ॳͷίϯϐϡʔλωοτϫʔΫ 1950೥୅ޙ൒ Semi-Automatic Ground Environment

  11. ֩౥ࡌരܸػͷܴܸͷܭࢉ https://www.youtube.com/watch?v=dF75jHfawtM

  12. ࠷ॳͷεʔύʔίϯϐϡʔλʔ 1964 CDC 6600 10MHz

  13. ೥ද 1946 ENIAC 1956 SAGE 1965 CDC6600 1971 Intel 4004

    740kHz 1957 Fortran 1958 LISP 1959 COBOL 1972 C, Smalltalk 1972 UNIX 1973 Ethernet 1969 ARPANET 50kbsp ͜ͷลͷࠒͷ࿩ˢ 1967 Thread
  14.  ݚڀॴ΍܉ࣄࢪઃʹ͸ɺҰൠՈఉͰ͍͏ͱ͜Ζͷ1990೥ ୅ʙ2000೥୅૬౰ͷίϯϐϡʔλͱωοτϫʔΫ؀ڥ͕ ͋ͬͨͱ૝૾͞ΕΔɻ ͳΔ΄Ͳɺࢲ͕ݟ͍ͯͨ2000೥લޙͷ࿩ͩͱࢥ͑͹ɺ
 ڞײͰ͖Δɻ

  15. Կ͕໰୊͔ͩͬͨʁ ϨʔείϯσΟγϣϯͷ༗໊ͳ໰୊ ۜߦޱ࠲ʹ10000ԁ͋Δ A͞Μ͕7000ԁҾ͖མͱͦ͏ͱ͢Δ B͞Μ͕8000ԁҾ͖མͱͦ͏ͱ͢Δ

  16. ͜Μͳίʔυ if Ҿ͖མͱֹ͠ <= ࢒ߴ ࢒ߴ -= Ҿ͖མͱֹ͠ return true

    else return false end
  17. ϚϧνεϨουͰಈ͔͢ͱʁ "͞Μ #͞Μ ޱ࠲ ࢒ߴ଍ΓΔʁ ࢒ߴ଍ΓΔʁ 0, 0, ԁҾ͖མͱ͠ ԁҾ͖མͱ͠

    ࢒ߴ10000ԁ ࢒ߴ͕ϚΠφεԁʂ
  18. ϩοΫͰղܾʁ ͜͜ͰϩοΫΛ͔͚Δ(1౓ʹ1ͭͷεϨου͔͠ڐ͞ͳ͍) if Ҿ͖མͱֹ͠ <= ࢒ߴ ࢒ߴ -= Ҿ͖མͱֹ͠ return

    true else return false end ͜͜ͰϩοΫΛղ์ ϩοΫΛ͠๨ΕΔ ղ์͠๨ΕΔ ΊΜͲ͍͘͞ ෆ҆
  19. ೥ද 1946 ENIAC 1956 SAGE 1965 CDC6600 1971 Intel 4004

    740kHz 1957 Fortran 1958 LISP 1959 COBOL 1972 C, Smalltalk 1972 UNIX 1973 Ethernet 1969 ARPANET 50kbsp ͜ͷลͷࠒͷ࿩ˢ 1967 Thread
  20. ΧϓηϧԽͯ͠σʔλΛकΔ ͷ͕ΦϒδΣΫτࢦ޲ͷ ໨తͷ1ͭͩͬͨͷʹ…

  21. ΞΫλʔϞσϧ શͯ͸ΞΫλʔͰ͋Δɻ(OOPͰ͍͏ͱ͜Ζͷʮશͯ͸ΦϒδΣΫτͰ͋Δʯͱࣅ͍ͯΔ) ΞΫλʔ͸ ܭࢉͰ͖Δɻ ༗ݶͷ৽͍͠ΞΫλʔΛੜ੒Ͱ͖Δɻ ଞͷΞΫλʔ΁༗ݶͷϝοηʔδΛૹΕΔɻ ࣍ʹड͚Δϝοηʔδ΁ͷ४උ͕Ͱ͖Δ(ঢ়ଶΛอ࣋Ͱ͖Δ)ɻ ϝʔϧϘοΫεΛ࣋ͪɺಧ͍ͨϝοηʔδΛ1ͭͣͭॱ൪ʹॲཧ͢Δɻ ※ϝοηʔδ͕ಧ͘ॱং͸ෆఆ ※ϝοηʔδ͸ಧ͔ͳ͍͔΋͠Εͳ͍

  22. ΞΫλʔϞσϧͰղܾ "͞Μ #͞Μ ޱ࠲ SFDFJWF 0, ԁҾ͖མͱ͠ ԁҾ͖མͱ͠ ࢒ߴ10000ԁ ϝʔϧϘοΫε

    ࢒ߴνΣοΫͯ͠ ԁҾ͖མͱ࣮͠ߦ /( SFDFJWF ϝοηʔδ͸ඞͣ
 1݅ͣͭॱ൪ʹ
 ॲཧ͞ΕΔ
  23. ΞΫλʔϞσϧ
 ʮΦϒδΣΫτʯͱಡΈସ͑ͯΈΔ શͯ͸ΞΫλʔͰ͋Δɻ ΞΫλʔ͸ ܭࢉͰ͖Δɻ ༗ݶͷ৽͍͠ΞΫλʔΛੜ੒Ͱ͖Δɻ ଞͷΞΫλʔ΁༗ݶͷϝοηʔδΛૹΕΔɻ ࣍ʹड͚Δϝοηʔδ΁ͷ४උ͕Ͱ͖Δ(ঢ়ଶΛอ࣋Ͱ͖Δ)ɻ ϝʔϧϘοΫεΛ࣋ͪɺಧ͍ͨϝοηʔδΛ1ͭͣͭॱ൪ʹॲཧ͢Δɻ ΦϒδΣΫτ

    ΦϒδΣΫτ ΦϒδΣΫτ ΦϒδΣΫτ ͚͕ͩ͜͜ OOPʹͳ͍ಛ௃
  24. ΞΫλʔϞσϧ͸ ΦϒδΣΫτࢦ޲ʹ ฒߦ࣮ߦʹର͢ΔΧϓηϧԽ Λ௥Ճͨ͠΋ͷͩͱ ߟ͑ΒΕΔ

  25. https://media.pragprog.com/titles/pb7con/actors.pdf Chapter5 Actors More Object-Oriented than Objects

  26. ΞΫλʔϞσϧͷ࢖͍ํ ݪٛతʹ͸ʮશͯΛΞΫλʔͱ͢ΔʯͷͰɺObjectΛnew͢ ΔΑ͏ͳؾ࣋ͪͰͲΜͲΜΞΫλʔΛ࡞͍͍ͬͯ(Erlang/ Elixir༻ޠͰݴ͏ͱɺprocessΛspawn͍͍ͯ͠)ɻ ΞΫλʔϞσϧ͸ΦϒδΣΫτࢦ޲ͱ΄΅ಉ͡ߟ͑ํͳͷ ͰɺΦϒδΣΫτࢦ޲ͷߟ͑ํΛྲྀ༻ͯ͠ϓϩάϥϜΛ૊ ΜͰ͍͍ɻσβΠϯύλʔϯͱ͔ɻ

  27. &-*9*3ͷίʔυྫ վ defmodule Greeter do def start do spawn(__MODULE__, :loop,

    []) end def hello(greeter, name) do send(greeter, {:hello, name}) end # ͜͜͸privateͬΆ͍ؾ࣋ͪ def loop do receive do {:hello, name} -> IO.puts "Hello #{name}" end loop() end end greeter = Greeter.start Greeter.hello(greeter, name) ࣮૷ͷৄࡉ ϓϩηεͷىಈͱ͔ɺ ϝοηʔδͷ΍ΓऔΓͷੜʑ͍͠ ͱ͜Ζ ͸Ӆ͢
  28. ͱݴΘΕ·ͯ͠΋ʜ ຊ౰ʹશ෦ΞΫλʔͰ͍͍ͷʁ Integer͢Β΋ʁ
 ˠຊ౰ʹඞવੑ͕͋ΔͷͰ͋Ε͹ɺ΍ͬͯ΋͍͍ɻ
 CounterΞΫλʔͱ͔Ͷɻ RailsͰΞϓϦ૊ΜͰΔͱThreadΛ࢖͍͍ͨ৔໘ͳΜͯ໓ଟʹͳ͍Μͩ ͚Ͳ…
 →Ϛετυϯ͸Sidekiqδϣϒͷմ
 https://github.com/tootsuite/mastodon/tree/master/app/workers

  29. ·ͱΊ ΞΫλʔϞσϧ͸ΦϒδΣΫτࢦ޲Λฒߦ؀ڥͰ΋ؾܰʹ࢖͑Δ Α͏ʹमਖ਼ͨ͠΋ͷͱߟ͑ΒΕΔͷͰɺΞΫλʔ͸༑ୡɺා͘ͳ ͍Α ιʔγϟϧωοτϫʔΫతͳػೳΛؚΉΞϓϦέʔγϣϯͰ͸ ΦϒδΣΫτάϥϑ͕ෳࡶʹͳΓ͕ͪͳͷͰ݁ߏ࢖͑Δ৔໘͕ ͋Δ ͦ͏͸ݴͬͯ΋ɺ࢖͍͗͢ΔͱύϑΥʔϚϯεͷ໰୊ͱ͔ಈ͖͕ ෆఆͰΘ͔Γʹ͘͘ͳͬͨΓ͢ΔͷͰઅ౓Λ࣋ͭඞཁ͕͋Γͦ͏