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

Python 3.6 Release Party async 関連

Python 3.6 Release Party async 関連

Tetsuya Morimoto

January 31, 2017
Tweet

Other Decks in Technology

Transcript

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

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

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

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

    ωΠςΟϒίϧʔνϯ (native coroutine) async def ߏจͰ࣮૷͞ΕΔ δΣωϨʔλʔϕʔεͷίϧʔνϯ (generator-based coroutine) δΣωϨʔλʔߏจͰ࣮૷͞ΕΔ ίϧʔνϯؔ਺ ίϧʔνϯΦϒδΣΫτΛฦؔ͢਺ɺasync ߏจΛ࢖͏
  5. 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
  6. 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! ࠓ೔ͷ࿩୊͸͜͜
  7. SAMPLE CODE Producer-Consumerύλʔϯ Λ࣮૷ͯ͠ΈΔ ੜ࢈ऀ (Producer): 3ਓɺ৯΂΋ͷΛςʔϒϧʹஔ͘ ফඅऀ (Consumer): 3ਓɺςʔϒϧʹ͋Δ΋ͷΛ৯΂Δ

    ςʔϒϧ: ৯΂΋ͷΛஔ͘৔ॴɺ1͔ͭ͠ஔ͚ͳ͍ e.g.) producer_consumer_pattern_generator_based_coroutine.py producer_consumer_pattern_native_coroutine.py
  8. ඇಉظ಺แදه (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
  9. 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
  10. JINJA 2.9 RELEASE Jinja 2.9 Released Python 3 Feature Support

    ͜ͷ࣮૷Λ͍ͯͯ͠ϒϩάΛॻ͍ͨΜͩͱࢥ͏ jinja2 Ͱ async/await Λϑϧαϙʔτ ςϯϓϨʔτΤϯδϯʹ coroutine function Λ౉ͤΔ
  11. 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