Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Async programming is all about programming synchronously.

Async programming is all about programming synchronously.

RubyKaigi 2013 - 10:00 room A
rev.7

40f4d1f2e77078955bd01e9fb4a503ba?s=128

seki at druby.org

June 01, 2013
Tweet

Transcript

  1. Async Asynchronously. ඇಉظͬͯಉظΛॻ͘͜ͱ͡ΌΜɻ seki@ruby-lang.org programming is all about programming

  2. Important First ϑολʔʹεϙϯαʔ͕ग़ͯΔ͔ΒݟͯͶ!!

  3. About me

  4. WEB+DB PRESS Vol.73 ʮͨͷ͍͠։ൃ࣮گதܧʯͷϨϏϡʔ͋Γ͕ͱ͏͍͟͝·ͨ͠ (ஶऀҰಉ) code ERB dRuby Rinda Drip

  5. ͑Ή͞ΜύΠϯΞϝ1kg͋Γ͕ͱ͏ talk RubyKaigi 2006, 7, 8, 9, 10, 11, 13

    RubyConf 2012
  6. ͺ͖ͬΎΔʙΜ!! @tsurumau and write dRuby ʹΑΔ ؔকढ़ஶ ෼ࢄ ɾ Web

    ϓϩάϥϛϯά
  7. Answering the RubyConf 2012

  8. ΦʔϜࣾץʰϓϩάϥϛϯάClojure ୈೋ൛ʱࢴͱిࢠॻ੶͋Θͤͯ޷ධൃചதʂ @koichiro RubyConf 2012 "dRuby͸ࣦഊ࡞ͩ" dRubyಉظݺͼग़͔ͩ͠Βࣦഊͳ ʁʁʁ ݴ͍༁͠ͳ͍ͱʂ

  9. RubyKaigi2013ָ͠ΈͰ͢ɻ@iwadon ࠓ೔͓࿩͢Δ͜ͱ ඇಉظʹ͓͜Δ͍ΖΜͳ͜ͱ͸͍ͭ΋͍ͩͨ ͍ಉ͡Α͏ʹղ͘ΜͩΑͬͯ࿩

  10. ΫϦΞίʔυ͸Rubyձٞ2013ͷγϧόʔεϙϯαʔͰ͢ ਐΊํ ੈք͸͍͍ͩͨඇಉظͱಉظͰͰ͖͍ͯΔ ඇಉظ෩ͳঢ়گΛͨ͘͞Μࣔ͠·͢ ͍ͭ΋ಉ͡Α͏ʹղ༷͘ࢠΛݟͯͶ

  11. ৿ా͞Μ͸ָͬͦ͘͢͝͠͏ʹ࿩Λฉ͍ͯ͘Ε·͢ɻൃදͷ͋ͱʹ͔ͭ·͑ͯ࿩ͯ͠ΈͯͶ ࠓ೔͓࿩͢Δ͜ͱ 2 ΋Μ͍͘͏ͳ dRuby͸ඇಉظݺͼग़͞Ε RubyͰͰ͖ΔόοΫάϥ΢ϯυॲཧ΋౰ વͰ͖Δͷ

  12. Wait a moment ✓ +3:00ɹ ✓ ࠓ೔ͷAgenda ✓ એ఻͗͢͠ͳ͍ ✓

    ࣍͸ඇಉظݺͼग़͞Ε
  13. ͙Μ·Ͱ·ͬͯΔ ඇಉظͬͯͳʹ Α͘Θ͔Μͳ͍͚Ͳಉظత͡Όͳ͍΋ͷʁ ࠓ೔͸ࣗ෼ͷ౎߹ͱؔ܎ͳ͘ى͜Δͳʹ͔ɺ ͱ͠·͢

  14. ࿩͔͚͠ͳ͍Ͱ΋Β͑·͔͢ @track8 ඇಉظݺͼग़͞Εܥ ࣗ෼ͷ౎߹ʹؔ܎ͳ͘ݺ͹ΕΔ UNIX signal dRuby RMI

  15. 9/21 ͱͪ͗Rubyձٞ05ΑΖ͓͘͠Ͷ͕͍͠·͢ @mame UNIX signal ͳʹΛ͍ͯͯ͠΋ɺಥવొ࿥͞Εͨؔ਺͕ݺ ͹Εͯɺ͖ͬ͞ͷͱ͜Ζ͔Β࠶։͢Δ ओॲཧ͸γάφϧ͕ൃੜͨ͜͠ͱΛ஌Βͳ͍

  16. ͕Μ͹ͬ΃౦๺! @MUNAKATA_Akiko UNIX signal ͳʹΛ͍ͯͯ͠΋ɺಥવొ࿥͞Εͨؔ਺͕ݺ ͹Εͯɺ͖ͬ͞ͷͱ͜Ζ͔Β࠶։͢Δ int foo() { while(1)

    { bar(); baz(); } } int signal_hanlder(...) { }
  17. Lang-8͸૬ޓఴ࡟ܕSNSͰɺ໿190ϲࠃ͔Β80छྨҎ্ͷݴޠͰར༻͞Εͯ·͢ɻ@sasata299 dRuby RMI dRuby͸ඇಉظݺͼग़͞ΕͰ͋Δ RMI͝ͱʹ৽͍͠εϨου͕Ͱ͖ͯॲཧ͢Δ ଞͷεϨου͸RMI͕͋ͬͨ͜ͱΛ஌Βͳ͍

  18. ΍Δؾφϯγʔ @vestige_ dRuby RMI RMI͝ͱʹ৽͍͠εϨου͕Ͱ͖ͯॲཧ͢Δ ଞͷεϨου͸RMI͕͋ͬͨ͜ͱΛ஌Βͳ͍ DRb.start_service(...) DRb.thread.join class Service

    def foo ... end end
  19. Yokohama.rb͸ຖ݄ୈ2౔༵೔։࠵Ͱ͢ʂ ඇಉظݺͼग़͞Εͷಛ௃ ·͞ʹඇಉظ ࣗ෼ͷ౎߹ʹؔ܎ͳ͘ݺͼग़͞ΕΔ ͦͯ͠ݺͼग़͞Εͨ͜ͱΛ஌Βͳ͍ ݺ͹ΕͨΒͳʹ͔͢Δɺ͕ॻ͖ʹ͍͘

  20. ֏͞Μڃʹ͸෺଍Γͳ͍ʰͳΔ΄ͲUNIXϓϩηεʱ͸ୡਓग़൛ձ͔Βେ޷ධൃചதͰ͢!! @kakutani ݺͼग़͞Εͨ͜ͱΛ఻͑Δ ఻͑Δνϟϯε͕͋Δͷ͸ݺͼग़͞Εͨଆ Ͳ͏΍ͬͯ఻͑Δʁ Global Variables Synchronization Mechanisms

  21. ελʔτΞοϓRuby Kindle൛͸͡Ί·ͨ͠ @igaiga555 Global Variables ඇಉظʹݺͼग़͞Εͨ͜ͱΛϝϞ͢Δ Ͱ΋ϝϞͨ͜͠ͱΛ଴ͯͳ͍ ϙʔϦϯά busy loopʹͳΔ͔΋...

    EINTRͱ͏·͘૊Έ߹ΘͤΔͷ΋ख
  22. hikari no cafe Ұॹʹߦ͜͏! ฏ೔ʹ༡ͼʹ͖ͯ!! > @ay Synchronization Mechanisms εϨουؒͷ଴߹ͤɺ఻ݴ

    ৚͕݅ἧ͏·ͰٳΉ εϨου͕ผεϨουʹ৘ใΛ఻͑Δ εϨου͕ผεϨου͔Βͷ৘ใΛड͚Δ
  23. @nagachika͞Μͷruby-trunk-changesɺ͍ͭ΋ݟͯ·͢! Latch, Semaphore, Monitor ৚͕݅ἧ͏·Ͱ଴ͭ ͋ͷඇಉظݺͼग़͕͋ͬͨ͠Αʂͱ͔Ͷ

  24. 6/5ͷAWS Summit Tokyo 2013Ͱ͠Ό΂Γ·͢ɻςΫχΧϧτϥοΫʹ͋ͿΕͨํ͸ͥͻɻ @tsuboi Queue ଴ͯΔ্ʹϞϊΛ౉ͤΔ FIFO ఻͍͑ͨΦϒδΣΫτΛpush, pop

    ۭͷͱ͖͸ΦϒδΣΫτ͕ಧ͘·ͰٳΉ
  25. ϑΣΠτɾςελϩοα http://www.fatechan.com Queue QueueΛ஥հʹೋͭεϨου͕ಉظ͢Δ while req = $q.pop do_it(req) end

  26. ϑΣΠτɾςελϩοα http://www.fatechan.com Queue QueueΛ஥հʹೋͭεϨου͕ಉظ͢Δ while req = $q.pop do_it(req) end

    class Service def foo(bar) $q.push(bar) end end
  27. ϑΣΠτɾςελϩοα http://www.fatechan.com Queue QueueΛ஥հʹೋͭεϨου͕ಉظ͢Δ while req = $q.pop do_it(req) end

    class Service def foo(bar) $q.push(bar) end end
  28. ϑΣΠτɾςελϩοα http://www.fatechan.com Queue QueueΛ஥հʹೋͭεϨου͕ಉظ͢Δ while req = $q.pop do_it(req) end

    class Service def foo(bar) $q.push(bar) end end sync!!!
  29. ϑΣΠτɾςελϩοα http://www.fatechan.com Queue QueueΛ஥հʹೋͭεϨου͕ಉظ͢Δ while req = $q.pop do_it(req) end

    class Service def foo(bar) $q.push(bar) end end sync!!!
  30. RubyKaigi 2013͸ແࣄϦϦʔε͞ΕͯͨΒiRubyKaigi'13ͰνΣοΫ!! @gutskun socket ... ಉظϝΧχζϜͱ͸ݺ͹Εͯͳ͍͚Ͳ... FIFO σʔλͷ౸ணΛ଴ͯΔ σʔλΛ౉ͤΔ

  31. @hemge ʹձͬͨͷ͸Rubyձٞ2007Ͱͨ͠Ͷʔɻࠓ౓ͦ͜দߐͷ࿨՛ࢠ԰͞ΜҊ಺͍ͯͩ͘͠͞ɻ Rinda::TupleSpace ॱংʹ͖·Γ͕ͳ͍ λϓϧͷऔΓग़͠ʹΑΔ଴߹ͤͱ৘ใަ׵ ύλʔϯϚον ActorϞσϧͷϝοηʔδͷج൫ʹ΋߹͏Α Τʔςϧ

  32. ΤϯδχΞืूதͰ͢ bit.ly/cookpad_jobs @mrkn Rinda::TupleSpace TupleSpaceΛத৺ʹෳ਺ͷεϨου͕ಉظ ΋͸΍ඇಉظݺͼग़͞Εؔ܎ͳ͍͚ͲӈଆͶ req = $ts.take([:req, ..])

    value = do_it(req) $ts.write([:ans, value])
  33. ΤϯδχΞืूதͰ͢ bit.ly/cookpad_jobs @mrkn Rinda::TupleSpace TupleSpaceΛத৺ʹෳ਺ͷεϨου͕ಉظ ΋͸΍ඇಉظݺͼग़͞Εؔ܎ͳ͍͚ͲӈଆͶ req = $ts.take([:req, ..])

    value = do_it(req) $ts.write([:ans, value]) $ts.write([:req, ..]) $ts.take([:ans, nil])
  34. ΤϯδχΞืूதͰ͢ bit.ly/cookpad_jobs @mrkn Rinda::TupleSpace TupleSpaceΛத৺ʹෳ਺ͷεϨου͕ಉظ ΋͸΍ඇಉظݺͼग़͞Εؔ܎ͳ͍͚ͲӈଆͶ req = $ts.take([:req, ..])

    value = do_it(req) $ts.write([:ans, value]) $ts.write([:req, ..]) $ts.take([:ans, nil]) sync!!!
  35. ΤϯδχΞืूதͰ͢ bit.ly/cookpad_jobs @mrkn Rinda::TupleSpace TupleSpaceΛத৺ʹෳ਺ͷεϨου͕ಉظ ΋͸΍ඇಉظݺͼग़͞Εؔ܎ͳ͍͚ͲӈଆͶ req = $ts.take([:req, ..])

    value = do_it(req) $ts.write([:ans, value]) $ts.write([:req, ..]) $ts.take([:ans, nil]) sync!!!
  36. ΤϯδχΞืूதͰ͢ bit.ly/cookpad_jobs @mrkn Rinda::TupleSpace TupleSpaceΛத৺ʹෳ਺ͷεϨου͕ಉظ ΋͸΍ඇಉظݺͼग़͞Εؔ܎ͳ͍͚ͲӈଆͶ req = $ts.take([:req, ..])

    value = do_it(req) $ts.write([:ans, value]) $ts.write([:req, ..]) $ts.take([:ans, nil]) sync!!! sync!!!
  37. ΤϯδχΞืूதͰ͢ bit.ly/cookpad_jobs @mrkn Rinda::TupleSpace TupleSpaceΛத৺ʹෳ਺ͷεϨου͕ಉظ ΋͸΍ඇಉظݺͼग़͞Εؔ܎ͳ͍͚ͲӈଆͶ req = $ts.take([:req, ..])

    value = do_it(req) $ts.write([:ans, value]) $ts.write([:req, ..]) $ts.take([:ans, nil]) sync!!! sync!!!
  38. Ұॹʹ͋ͷөըΛݟ͍ͨͰ͢ɻ > @nari3 ·ͱΊ ඇಉظʹݺͼग़͞Εͨ݁ՌΛ஌Δͷ͸໘౗ ಉظϝΧχζϜΛ࢖͏ͱ఻͑Δ͜ͱ͕Ͱ͖Δ

  39. Wait a moment ✓ +13:00ɹ ✓ dRuby͸ඇಉظݺͼग़͞Ε ✓ ݺͼग़͞Εͨ݁ՌΛผεϨουʹ఻͑Δಉظ ✓

    ࣍͸ଈ࣌෮ؼ
  40. ֏͞ΜͷಠಛͷงғؾಌΕ·͢! ͍͔ͭ࿩ֻ͚͠ʹ͍͖·͢! - @elim όοΫάϥ΢ϯυॲཧ ଈ࣌෮ؼ ॲཧΛड͚෇͚ͨΒ੍ޚ͕໭Δ ड͚෇͚ॲཧͩͱࢥ͏ͱ׬ྃͯ͠Δ

  41. The dRuby Book ೔ຊޠ൛͸δϡϯΫಊͰۚͰങ͑Δ ͜Ε͸ඇಉظʁ ͏ʔΜ ࣗ෼ͷ౎߹ͱؔ܎ͳ͘ॲཧΛ࣮ߦ ࣗ෼ͷ౎߹ͱؔ܎ͳ͘׬ྃ

  42. 9/21 ಹਢԘݪ ͱͪ͗Rubyձٞ05 Eiffelຊ༁ऀͷञ೏͞Μ͕͘ΔΑʂ ݁Ռ͕ؾʹͳΔ ଈ࣌෮ؼͷଟ͘ͷ৔߹ ΍Γͬͺͳ͠Ͱ͸ͳ͘ ݁Ռʹڵຯ͕͋Δ ݁ՌΛ଴ͭAPI͕༻ҙ͞ΕͯΔ

  43. The dRuby Book Japanese Edition : Best as GIFT FROM

    JAPAN جຊతͳύλʔϯ ॲཧΛґཔ → νέοτ΋Β͏! ฒߦͯࣗ͠෼ͷ࢓ࣄΛ͢Δ νέοτ࢖ͬͯґཔͷ݁ՌΛ଴ͭ←ಉظ͢Δ
  44. ` service جຊతͳύλʔϯ d request join ticket value application background

    task
  45. dRubyHiroba - dRuby hands-on 6/2 10:30- (RubyHiroba) νέοτͷྫ ιέοτͦͷ΋ͷ͕νέοτ ґཔΛॻ͖ɺ݁ՌΛಡΉ

    popenͳͲ΋ࣅͨΑ͏ͳ΋ͷ ticket = TCPClient.new(...) ticket.write('your task') my_important_task ticket.read
  46. 2013-9-21 ͱͪ͗Rubyձٞ05 @ ಹਢԘݪࢢ౦ಹਢ໺ެຽؗ νέοτͷྫ εϨου΋νέοτͷΑ͏ʹ࢖͑Δ joinͰ଴߹ͤͱ஋ͷऔಘ͕Ұ౓ʹͰ͖ΔΑ ticket = Thread.new

    { your_task } my_important_task ticket.join
  47. The dRuby Book νέοτͷྫ λϓϧεϖʔεͳΒλϓϧ ticket = Object.new tuple_space.write([:your_task, ticket])

    my_important_task tuple_space.take([:your_task_done, ticket])
  48. ʮαϯϥΠζग़ӢͰߦ͘ RubyWorld Conference 2013 ʯಉߦऀืूதʂ @yancya ΋ࣗ͠෼͕αʔόͩͬͨΒ ॲཧΛґཔ ฒߦͯࣗ͠෼ͷ࢓ࣄ͢Δ νέοτ࢖ͬͯґཔͷ݁ՌΛ଴ͭ

  49. ʮαϯϥΠζग़ӢͰߦ͘ RubyWorld Conference 2013 ʯಉߦऀืूதʂ @yancya ΋ࣗ͠෼͕αʔόͩͬͨΒ ॲཧΛґཔ ฒߦͯࣗ͠෼ͷ࢓ࣄΛ͢Δ νέοτ࢖ͬͯґཔͷ݁ՌΛ଴ͭ

    ґཔΛ଴ͭ
  50. ʮαϯϥΠζग़ӢͰߦ͘ RubyWorld Conference 2013 ʯಉߦऀืूதʂ @yancya ΋ࣗ͠෼͕αʔόͩͬͨΒ ॲཧΛґཔ ฒߦͯࣗ͠෼ͷ࢓ࣄΛ͢Δ νέοτ࢖ͬͯґཔͷ݁ՌΛ଴ͭ

    ґཔΛ଴ͭ ೋछྨ଴͍ͪͨ
  51. dRubyHiroba - dRuby hands-on 6/2 10:30- (RubyHiroba) ଴ͪํ͕·ͪ·ͪͩͱࠔΔ or Ͱ͸ͭͳ͛ͳ͍

    ࠷ॳͷ଴߹ͤͰࢭ·ͬͪΌ͏ ͍Ζ͍ΖҰ౓ʹ଴ͯͳ͍ͱμϝ queue.pop or soc.read or rinda.take or ...
  52. dRubyHiroba - dRuby hands-on 6/2 10:30- (RubyHiroba) ҰൠԽͨ͠αʔόͷϧʔϓ ͍Ζ͍Ζ·ͱΊͯ଴ͭ ৽͍͠ґཔͳΒ...

    ׬ྃ௨஌ͳΒ...
  53. dRubyHiroba - dRuby hands-on 6/2 10:30- (RubyHiroba) ۉ࣭ʹͯ͠·ͱΊͯ଴ͭ ͲΕ΋ಉ͡APIͰ଴ͯΔΑ͏ʹۉ࣭ʹ͢Δ select()͸શͯΛϑΝΠϧʹͯ͠Δ

    TupleSpace͸tupleʹਖ਼نԽ
  54. dRubyHiroba - dRuby hands-on 6/2 10:30- (RubyHiroba) ·ͱΊ ଈ࣌෮ؼ͢Δͱ͖ɺ͍͍ͨͯ݁Ռ͕ؾʹͳΔ ݁ՌΛ଴ͭͷ͸ಉظ

    ඇಉظݺͼग़͞Εͱಉ͡Α͏ͳײ͡
  55. Wait a moment ✓ +22:00ɹ ✓ ଈ࣌෮ؼͰ΋ಉظ͕ϛι ✓ ࣍͸ࣗ෼͚ͩ͸ݻ·Βͳ͍౒ྗͷ͜ͱ

  56. Ruby's GC slide is here! : slidesha.re/19sbBwA (w/o animation, free

    ver) and gumroad.com/l/xWCR (w/ animation, premium ver) selectଐϝΠϯϧʔϓ select(ͨ͘͞Μͷιέοτ) ϒϩοΫ͠ͳ͍ιέοτͷಡΈॻ͖ ͳʹ͔ॲཧΛ͢Δ
  57. ͵Ruby൪௕ɺ͍ͭ΋͓ੈ࿩ʹͳ͓ͬͯΓ·͢ʂTIMEXɺ͔Θ͍͍Ͱ͋Γ·͢ʂ ίʔϧόοΫͷύλʔϯ select(ͨ͘͞Μͷιέοτ) ϒϩοΫ͠ͳ͍ιέοτͷಡΈॻ͖ ͳʹ͔ॲཧΛ͢Δ ϑϨʔϜϫʔΫ͕Ӆ͢ ͜ͷลίʔϧόοΫ

  58. The dRuby Book ίʔϧόοΫͷ᠘ select(ͨ͘͞Μͷιέοτ) ϒϩοΫ͠ͳ͍ιέοτͷಡΈॻ͖ ͳʹ͔ॲཧΛ͢Δ ϑϨʔϜϫʔΫશମ͕஗͘ ͳΔ ͕͜͜஗͍ͱ

  59. The dRuby Book ίʔϧόοΫͷ᠘ select(ͨ͘͞Μͷιέοτ) ϒϩοΫ͠ͳ͍ιέοτͷಡΈॻ͖ ͳʹ͔ॲཧΛ͢Δ ૬ޓʹݺͼ߹͍ͬͯΔͱγεςϜશ ମ͕ݻ·Δ... ͞Βʹผαʔόʹґཔͨ͘͠

    ͯ΋͜͜Ͱ݁ՌΛ଴ͭͱ
  60. The dRuby Book ίʔϧόοΫͷ᠘ select(ͨ͘͞Μͷιέοτ) ϒϩοΫ͠ͳ͍ιέοτͷಡΈॻ͖ ͳʹ͔ॲཧΛ͢Δ ͠ΐ͏͕ͳ͍͔Β݁Ռ଴ͪ΋select ϑϨʔϜϫʔΫͷ؅ཧ֎Ͱ଴ͭ͜ͱʹ

  61. http://www.amazon.co.jp/registry/wishlist/1R43BBPSPUEEE/ γϯάϧεϨου೿ ॲཧ͕Ͳ͜·ͰਐΜ͔ͩίϯςΩετΛ࡞ͬ ͯɺॲཧதͰ΋ϝΠϯϧʔϓʹ໭Δ ࣗવͳίʔυʹ͢Δͷ͸ۤ࿑͢Δ Fiberͱ͔ܧଓͱ͔...

  62. 2013-9-21ͱͪ͗Rubyձٞ05 @ ಹਢԘݪࢢ౦ಹਢ໺ެຽؗ ͋Εʁ ιέοτͷ଴ͪͱFiberͳΒThreadͱେࠩͳ ͍ͷͰ͸ʁ

  63. 2013-9-21ͱͪ͗Rubyձٞ05 @ ಹਢԘݪࢢ౦ಹਢ໺ެຽؗ ϚϧνεϨουͰղ͘ acceptͨ͠ΒThreadੜ੒ͯ͠readɺॲཧɺ write͢Δ ίϯςΩετͷอଘ΍෮ݩ͕ෆཁ ࣗવͳίʔυ͕ॻ͚Δ ௕͍ॲཧ΋Ͱ͖Δ͠ɺผαʔϏεʹ׬ྃ ෮ؼͰґཔͯ͠΋ྑ͍

  64. 2013-9-21ͱͪ͗Rubyձٞ05 @ ಹਢԘݪࢢ౦ಹਢ໺ެຽؗ dRubyͷ࣮૷ͦͷ΋ͷ େࣄͳΦνͰ͢Α!! Rubyʹ͓͚Διέοταʔόͷయܕ shttpsrv (ݪ͞ΜͷHTTPαʔόʔ) ඇৗʹૉ๿ dRuby͸ͦͷΑ͏ʹ࡞ΒΕ͍ͯΔ

  65. dRubyHiroba - dRuby hands-on 6/2 10:30- (RubyHiroba) ·ͱΊ ඇಉظʹ͓͜Δ͍ΖΜͳ͜ͱ͸͍ͭ΋͍ͩͨ ͍ಉ͡Α͏ʹղ͘ΜͩΑͬͯ࿩Ͱͨ͠

  66. dRubyHiroba - dRuby hands-on 6/2 10:30- (RubyHiroba) ͱͪ͗RubyKaigi 05 2013-9-21

    ಹਢԘݪ ট଴ߨԋ: ञ೏׮