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

Python 3.6 Release Party async 関連

Python 3.6 Release Party async 関連

0ab449b0232c256482723f523b4612db?s=128

Tetsuya Morimoto

January 31, 2017
Tweet

Other Decks in Technology

Transcript

  1. ©OVERDUEBOOK Python 3.6 Release Party ~asynchronous related~ 2017-01-31 Tue. Tetsuya

    Morimoto
  2. INTRODUCTION ৿ຊ఩໵ (@t2y) ϙʔλϧαΠτͷձࣾ ੲ͸αʔόʔαΠυΤϯδχΞ ࠓ͸ΠϯϑϥΤϯδχΞݟश͍ new! ͓࢓ࣄͰ͸ओʹ Go ॻ͍͍ͯΔ͚ͲɺPython

    ΋࢖͍·͢ ࠷ۙ΍ͬͨ͜ͱ [຋༁]αΠτ৴པੑΤϯδχΞϦϯά (SRE) ͱ͸Կ͔ʁ
  3. AGENDA ඇಉظʗฒߦॲཧͷഎܠ Python 3 ͷඇಉظॲཧͷมભ asyncio ϞδϡʔϧΛ࢖ͬͯΈΑ͏ Python 3.6 ͷඇಉظॲཧͷ৽ػೳ

    (PEP-525/530) asyncio ͷ՝୊ʁ ·ͱΊ
  4. ATTENTION ͜͜Ͱ঺հ͢Δࢿྉ΋ίʔυ΋ެ։͠·͢ ࢲ͕ asyncio ΍ؔ࿈ٕज़ʹૄ͍ͨΊɺҰੜݒ໋ௐ΂ͨ͜ͱΛ ·ͱΊͨͭ΋ΓͰ͕͢ɺؒҧ͍ͬͯͨΒ͝ΊΜͳ͍͞

  5. ඇಉظʗฒߦॲཧͷഎܠ

  6. ASYNCHRONOUS APPROACH ϚϧνεϨου vs. Πϕϯτۦಈ ෼͔Γ΍͍͢ൺֱ Apache vs. Nginx Java

    (JVM) vs. Node.js ͲͪΒ͕ྑ͍ͱ͍͏Θ͚Ͱ͸ͳ͘Ξϓϩʔνͷҧ͍ ΞϓϦέʔγϣϯͷಛੑʹΑͬͯ޲͖ෆ޲͖͸͋Δʁ ࠓ೔ͷ࿩୊͸ͬͪ͜
  7. CONCURRENT & PARALLEL ฒߦ (concurrent) ͱฒྻ (parallel) ݫີͳఆٛ͸ͳ͍ Rob Pike

    ࢯͷߨԋʹΑΔͱɺ͜ΜͳΠϝʔδˣ ࠓ೔ͷ࿩୊͸ͬͪ͜
  8. EXECUTABLE PROGRAM ࣮ߦϓϩάϥϜͷॲཧͷ෼ׂ୯Ґ ϓϩηε (Process) OS ͕؅ཧɺಠࣗͷϝϞϦۭؒ (ωΠςΟϒ)εϨου (Thread) OS

    ͕؅ཧɺಉҰϓϩηε಺ͰϝϞϦۭؒΛڞ༗ ίϧʔνϯ (Coroutine) ≒ ϑΝΠόʔʗδΣωϨʔλʔ (ػೳతʹ͸ಉ͡) ॲཧΛ్தͰҰ࣌தஅͨ͠Γɺ్த͔Β෮ݩͨ͠Γ͠ͳ͕Β࣮ߦͰ͖Δ ݴޠ (ΞϓϦ) ͕؅ཧ ϓϩηε1 εϨου1 εϨου2 ϓϩηε2
  9. SEMANTICS IN PYTHON 3 δΣωϨʔλʔ yield, yield from Λ࢖ͬͯఆٛͨؔ͠਺ ίϧʔνϯ

    ωΠςΟϒίϧʔνϯ (native coroutine) async def ߏจͰ࣮૷͞ΕΔ δΣωϨʔλʔϕʔεͷίϧʔνϯ (generator-based coroutine) δΣωϨʔλʔߏจͰ࣮૷͞ΕΔ ίϧʔνϯؔ਺ ίϧʔνϯΦϒδΣΫτΛฦؔ͢਺ɺasync ߏจΛ࢖͏
  10. PYTHON 3 ͷඇಉظॲཧͷมભ

  11. HISTORY (1) 3.3 yield from ߏจ δΣωϨʔλʔ಺෦ͰαϒδΣωϨʔλʔ΁ͷҠৡ PEP 380 --

    Syntax for Delegating to a Subgenerator 3.4 asyncio ϞδϡʔϧΛඪ४ϥΠϒϥϦ΁ (࢑ఆ) ඇಉظʗฒߦॲཧΛ࣮૷͢ΔͨΊͷΠϯϑϥ PEP 3156 -- Asynchronous IO Support Rebooted: the "asyncio" Module
  12. HISTORY (2) 3.5 async ߏจͱ await ࣜ ωΠςΟϒίϧʔνϯΛಋೖ PEP 492

    -- Coroutines with async and await syntax 3.6 asyncio Ϟδϡʔϧͷ API ͕࢑ఆ → ҆ఆ΁ ඇಉظδΣωϨʔλʔͱඇಉظ಺แදهΛಋೖ PEP 525 -- Asynchronous Generators PEP 530 -- Asynchronous Comprehensions new! ࠓ೔ͷ࿩୊͸͜͜
  13. ASYNCIO ϞδϡʔϧΛ࢖͏

  14. USE CASE ίϧʔνϯͬͯͲ͏͍͏ͱ͖ʹ࢖͏ͷʁ ڠௐతϚϧνλεΫɺྫ֎ɺΠϕϯτϧʔϓɺΠςϨʔ λʔɺແݶϦετɺύΠϓͱ͍ͬͨॲཧΛ࣮૷͢Δͱ͖ ར༻ྫ ঢ়ଶϚγϯ ΞΫλʔϞσϧ CSP (Communicating

    Sequential Processes)
  15. SAMPLE CODE Producer-Consumerύλʔϯ Λ࣮૷ͯ͠ΈΔ ੜ࢈ऀ (Producer): 3ਓɺ৯΂΋ͷΛςʔϒϧʹஔ͘ ফඅऀ (Consumer): 3ਓɺςʔϒϧʹ͋Δ΋ͷΛ৯΂Δ

    ςʔϒϧ: ৯΂΋ͷΛஔ͘৔ॴɺ1͔ͭ͠ஔ͚ͳ͍ e.g.) producer_consumer_pattern_generator_based_coroutine.py producer_consumer_pattern_native_coroutine.py
  16. PYTHON 3.6 ͷඇಉظॲཧͷ৽ػೳ (PEP-525/530)

  17. ඇಉظδΣωϨʔλʔ (PEP-525) ඇಉظδΣωϨʔλʔΛ yield Λ࢖ͬͯఆٛͰ͖Δ ͜Ε·ͰͷδΣωϨʔλʔΛఆٛ͢Δͷͱಉ͡ײ֮ ͓खܰɺ׌ͭ2ഒ଎͍ e.g.) async_iter_ticker.py async_for_example_pep492.py

    async_for_example_pep525.py
  18. ඇಉظ಺แදه (PEP 530) ඇಉظ಺แදهͷهड़ set: {await afun(i) async for i

    in agen()} list: [await afun(i) async for i in agen()] dict: {i: await afun(i ** 2) async for i in agen()} generator: (await afun(i ** 2) async for i in agen()) ͜ΕΒ͸ async def Ͱఆٛ͞Εͨؔ਺಺ͰͷΈར༻Ͱ͖Δ ඇಉظδΣωϨʔλʔࣜ͸ 3.7 ͰͲ͜Ͱ΋࢖͑ΔΑ͏ʹͳΔ e.g.) async_comprehension_example.py
  19. ASYNCIO ͷ՝୊ʁ

  20. ARMIN RONACHER’S BLOG I don’t understand Python’s Asyncio ͱ͍͏ϒϩάهࣄ twisted

    ͔Β֓೦ΛऔΓೖΕ͍ͯΔʁ δΣωϨʔλʔͷઃܭϛε e.g.) surprising_generator.py ෳ਺ͷΠϕϯτϧʔϓΛෳ਺εϨουͰѻ͏ͱ͖ʹ໌ࣔతͱ͸ݴ͑ͳ͍Ͷ ΠϕϯτϧʔϓͰ subprocess Λ࣮ߦͯͦ͠ͷγάφϧΛัଊͯ͠ dispatch ͢Δͱ͔ ϩʔΧϧσʔλΛѻ͏ context ͷ֓೦͕ͳ͍ΑͶ ͦ΋ͦ΋ asyncio ʹ context ͷ֓೦͕ͳ͍ asyncio ͷ࠷ѱͳͱ͜Ζ͸େͯ͠଎͘ͳ͍ David Beazley ࢯͷ curio ͸ asyncio ΑΓ75-150% ΄Ͳ଎͍ https://github.com/dabeaz/curio
  21. JINJA 2.9 RELEASE Jinja 2.9 Released Python 3 Feature Support

    ͜ͷ࣮૷Λ͍ͯͯ͠ϒϩάΛॻ͍ͨΜͩͱࢥ͏ jinja2 Ͱ async/await Λϑϧαϙʔτ ςϯϓϨʔτΤϯδϯʹ coroutine function Λ౉ͤΔ
  22. ·ͱΊ

  23. SUMMARY ඇಉظʗฒߦॲཧͷ͜ͱ ຊ࣭తʹ೉͍͠ॲཧͷ1ͭ ݴޠॲཧܥ͕ϓϩάϥϚʔΛαϙʔτ: async/await Python 3 ʹ͔͠ͳ͍৽ػೳ 2→3 ҠߦΛଅ͢ຊ໋ʁ

    (≠ ϓϩύΨϯμ) 3.6 Ͱػೳ͕Ұஈམʁ asyncio Ϟδϡʔϧͷ API ͕҆ఆ൛΁ ඇಉظδΣωϨʔλʔ
  24. REFERENCE ൃදͰ঺հͨ͠αϯϓϧίʔυ https://github.com/t2y/python-study/tree/master/ Python36ReleaseParty/async ϚϧνίΞ࣌୅ͷ࠷৽ฒྻฒߦٕज़ Overview of Modern Concurrency and

    Parallelism Concepts 18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks pymotw3: asyncio — Asynchronous I/O, event loop, and concurrency tools