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

ASGIアプリケーション入門 - こわくないasyncio基礎と非同期IO

ASGIアプリケーション入門 - こわくないasyncio基礎と非同期IO

PyConJP2021の登壇資料です。

Junya Fukuda

October 15, 2021
Tweet

More Decks by Junya Fukuda

Other Decks in Programming

Transcript

  1. •෱ా ൏໵ʢJunya Fukudaʣʢ@JunyaFffʣ •גࣜձࣾ೔ຊγεςϜٕݚʢJSLʣॴଐ ௕໺ݝͷձࣾ •WebΤϯδχΞ •GEEKLAB.NAGANO - ίϛϡχςΟεϖʔεӡӦ •Effective

    Python ͷಡॻձ΍ͬͯ·͢ 📚 •ʢ͓͠͝ͱͰʣόϦϡʔϒοΫε •ຊ޷͖ʹѪ͞ΕΔαʔϏεΛ໨ࢦͯ͠ʢݹຊങऔɾൢചʣ ΪʔΫϥϘ௕໺ಡॻձ •GEEKLAB.NAGANO - ίϛϡχςΟεϖʔεӡӦ
  2. って 2018-12-12 2019-04-19 2019-08-26 2020-01-01 2020-05-08 2020-09-13 2021-01-20 2021-05-28 2021-10-03

    Time 0 4700 9300 13900 18500 23200 27800 32400 37000 Stargazers •ελʔ਺ͷભҠ https://starchart.cc/tiangolo/fastapi
  3. って •Fast: Very high performance, on par with NodeJS and

    Go ʢߴ଎: NodeJS ΍ Go ฒΈͷͱͯ΋ߴ͍ύϑΥʔϚϯεʣ https://fastapi.tiangolo.com/ •ߴύϑΥʔϚϯε
  4. って •Fast: Very high performance, on par with NodeJS and

    Go ʢߴ଎: NodeJS ΍ Go ฒΈͷͱͯ΋ߴ͍ύϑΥʔϚϯεʣ https://fastapi.tiangolo.com/ •ߴύϑΥʔϚϯε •͍Ζ͍Ζͳ֯౓͕͋Δ
  5. って •Fast: Very high performance, on par with NodeJS and

    Go ʢߴ଎: NodeJS ΍ Go ฒΈͷͱͯ΋ߴ͍ύϑΥʔϚϯεʣ https://fastapi.tiangolo.com/ •ߴύϑΥʔϚϯε •͍Ζ͍Ζͳ֯౓͕͋Δ - ڞ௨͢Δͷ͸ඇಉظϓϩάϥϛϯά
  6. って •Fast: Very high performance, on par with NodeJS and

    Go ʢߴ଎: NodeJS ΍ Go ฒΈͷͱͯ΋ߴ͍ύϑΥʔϚϯεʣ https://fastapi.tiangolo.com/ •ߴύϑΥʔϚϯε •͍Ζ͍Ζͳ֯౓͕͋Δ - ڞ௨͢Δͷ͸ඇಉظϓϩάϥϛϯά •FastAPIͬͯඇಉظʁ
  7. って •Fast: Very high performance, on par with NodeJS and

    Go ʢߴ଎: NodeJS ΍ Go ฒΈͷͱͯ΋ߴ͍ύϑΥʔϚϯεʣ https://fastapi.tiangolo.com/ •ߴύϑΥʔϚϯε •͍Ζ͍Ζͳ֯౓͕͋Δ - ڞ௨͢Δͷ͸ඇಉظϓϩάϥϛϯά •FastAPIͬͯඇಉظʁ •Webͷ෦෼͸ɺStarlette
  8. って •Fast: Very high performance, on par with NodeJS and

    Go ʢߴ଎: NodeJS ΍ Go ฒΈͷͱͯ΋ߴ͍ύϑΥʔϚϯεʣ https://fastapi.tiangolo.com/ •ߴύϑΥʔϚϯε •͍Ζ͍Ζͳ֯౓͕͋Δ - ڞ௨͢Δͷ͸ඇಉظϓϩάϥϛϯά •FastAPIͬͯඇಉظʁ •Webͷ෦෼͸ɺStarlette = ASGIରԠͷWeb FW ASGIରԠ
  9. •WebαʔόͱϑϨʔϜϫʔΫΛͭͳ͙ɺ࢓༷ •WSGIͷਫ਼ਆతޙܧ •WSGI って? ASGIରԠ •αʔόɿGunicorn uWSGI ⁶ ϑϨʔϜϫʔΫɿDjango, Flask,

    Pyramid •ASGI •αʔόɿUvicorn, Hypercorn ⁶ ϑϨʔϜϫʔΫɿDjango, FastAPI, Starlette •WSGIͱASGIɺͳʹ͕ҧ͏ͷ͔
  10. ASGI対応 •WebαʔόͱϑϨʔϜϫʔΫΛͭͳ͙ɺ࢓༷ •WSGIͷਫ਼ਆతޙܧ •Gunicorn ΍ uWSGI ⁶ Django ΍ Flask,

    Pyramid •Uvicorn ΍ Hypercorn ⁶ Django ΍ FastAPI, Starlette って? asyncioに対応したインターフェース仕様
  11. •Python 3.4 Ͱ ௥Ճ͞Εͨඪ४ϥΠϒϥϦ •Python 3.6 Ͱ ඇಉظδΣωϨʔλʔ / ඇಉظ಺แදه

    •Python 3.5 Ͱ async/await ߏจ asyncioはこわくない •ฒߦॲཧͷ ίʔυΛॻͨ͘ΊͷϥΠϒϥϦ
  12. •Python 3.4 Ͱ ௥Ճ͞Εͨඪ४ϥΠϒϥϦ •Python 3.6 Ͱ ඇಉظδΣωϨʔλʔ / ඇಉظ಺แදه

    •Python 3.5 Ͱ async/await ߏจ asyncioはこわくない •ฒߦॲཧͷ ίʔυΛॻͨ͘ΊͷϥΠϒϥϦ •γϯάϧεϨουͰΠϕϯτϧʔϓͰඇಉظ IO
  13. •Python 3.4 Ͱ ௥Ճ͞Εͨඪ४ϥΠϒϥϦ •Python 3.6 Ͱ ඇಉظδΣωϨʔλʔ / ඇಉظ಺แදه

    •Python 3.5 Ͱ async/await ߏจ asyncioはこわくない •ฒߦॲཧͷ ίʔυΛॻͨ͘ΊͷϥΠϒϥϦ •γϯάϧεϨουͰΠϕϯτϧʔϓͰඇಉظ IO •ࠓ೥RFCͰඪ४Խ͞ΕͨWebن֨ QUIC ͷॳظͷϦϑΝϨϯεత࣮૷ aioquic
  14. •͜Θ͔ͬͨ •Using Asyncio in Python - ΦϥΠϦʔ ʢ🐸ຊʣ asyncio めっちゃこわい

    •2ͭͷग़ձ͍ https://www.oreilly.com/library/view/using-asyncio-in/9781492075325/
  15. •͜Θ͔ͬͨ •Using Asyncio in Python - ΦϥΠϦʔ ʢ🐸ຊʣ asyncio めっちゃこわい

    •2ͭͷग़ձ͍ https://www.oreilly.com/library/view/using-asyncio-in/9781492075325/ https://blog.jrfk.dev/posts/using-asyncio-in-python/
  16. •͜Θ͔ͬͨ •ίϧʔνϯ͸ා͘ͳ͍ - Minimum Viable Programmer ʢϒϩάʣ •Using Asyncio in

    Python - ΦϥΠϦʔ ʢ🐸ຊʣ asyncio めっちゃこわい •2ͭͷग़ձ͍
  17. •͜Θ͔ͬͨ •ίϧʔνϯ͸ා͘ͳ͍ - Minimum Viable Programmer ʢϒϩάʣ •Using Asyncio in

    Python - ΦϥΠϦʔ ʢ🐸ຊʣ asyncio めっちゃこわい •2ͭͷग़ձ͍ •࣮૷Ͱ஌Δasyncio -Πϕϯτϧʔϓͷਖ਼ମͱ͸ - REI SUYAMA ͞Μ •DAY2 - 10.16(SAT) 16:20 - #pyconjp_2
  18. •͜Θ͔ͬͨ •ίϧʔνϯ͸ා͘ͳ͍ - Minimum Viable Programmer ʢϒϩάʣ •Using Asyncio in

    Python - ΦϥΠϦʔ ʢ🐸ຊʣ asyncio めっちゃこわい •2ͭͷग़ձ͍ •࣮૷Ͱ஌Δasyncio -Πϕϯτϧʔϓͷਖ਼ମͱ͸ - REI SUYAMA ͞Μ •DAY2 - 10.16(SAT) 16:20 - #pyconjp_2
  19. •Python 3.4 Ͱ ௥Ճ͞Εͨඪ४ϥΠϒϥϦ •Python 3.6 Ͱ ඇಉظδΣωϨʔλʔ / ඇಉظ಺แදه

    •Python 3.5 Ͱ async/await ߏจ asyncioはこわくない こわい •ฒߦॲཧͷ ίʔυΛॻͨ͘ΊͷϥΠϒϥϦ •γϯάϧεϨουͰΠϕϯτϧʔϓͰඇಉظ IO •ઌ೔RFCͰඪ४Խ͞ΕͨWebن֨ QUIC ͷϦϑΝϨϯεత࣮૷ aioquic
  20. •Python 3.4 Ͱ ௥Ճ͞Εͨඪ४ϥΠϒϥϦ •Python 3.6 Ͱ ඇಉظδΣωϨʔλʔ / ඇಉظ಺แදه

    •Python 3.5 Ͱ async/await ߏจ asyncioはこわくない こわい •ฒߦॲཧͷ ίʔυΛॻͨ͘ΊͷϥΠϒϥϦ •γϯάϧεϨουͰΠϕϯτϧʔϓͰඇಉظ IO •ઌ೔RFCͰඪ४Խ͞ΕͨWebن֨ QUIC ͷϦϑΝϨϯεత࣮૷ aioquic
  21. •Python 3.4 Ͱ ௥Ճ͞Εͨඪ४ϥΠϒϥϦ •Python 3.6 Ͱ ඇಉظδΣωϨʔλʔ / ඇಉظ಺แදه

    •Python 3.5 Ͱ async/await ߏจ asyncioはこわくない こわい •ฒߦॲཧͷ ίʔυΛॻͨ͘ΊͷϥΠϒϥϦ •γϯάϧεϨουͰΠϕϯτϧʔϓͰඇಉظ IO •ઌ೔RFCͰඪ४Խ͞ΕͨWebن֨ QUIC ͷϦϑΝϨϯεత࣮૷ aioquic
  22. 並⾏処理 注⽂を取る - 料理を作る - 料理を運ぶ 注⽂を取る - 料理を作る -

    料理を運ぶ 注⽂を取る - 料理を作る - 料理を運ぶ
  23. 並⾏処理 注⽂を取る - 料理を作る - 料理を運ぶ 注⽂を取る - 料理を作る -

    料理を運ぶ 注⽂を取る - 料理を作る - 料理を運ぶ
  24. 並⾏処理 注⽂を取る - 料理を作る - 料理を運ぶ 注⽂を取る - 料理を作る -

    料理を運ぶ 注⽂を取る - 料理を作る - 料理を運ぶ asyncio シングルスレッド
  25. 並⾏処理 注⽂を取る - 料理を作る - 料理を運ぶ 注⽂を取る - 料理を作る -

    料理を運ぶ 注⽂を取る - 料理を作る - 料理を運ぶ asyncio シングルスレッド = ワンオペ
  26. 並⾏処理 注⽂を取る - 料理を作る - 料理を運ぶ 注⽂を取る - 料理を作る -

    料理を運ぶ 注⽂を取る - 料理を作る - 料理を運ぶ asyncio シングルスレッド = ワンオペ 同じ⼈
  27. 並⾏処理 注⽂を取る - 料理を作る - 料理を運ぶ 注⽂を取る - 料理を作る -

    料理を運ぶ 注⽂を取る - 料理を作る - 料理を運ぶ asyncio シングルスレッド = ワンオペ どのタイミングでタスクの切り替えをするのでしょうか 🤔
  28. •Python 3.4 Ͱ ௥Ճ͞Εͨඪ४ϥΠϒϥϦ •Python 3.6 Ͱ ඇಉظδΣωϨʔλʔ / ඇಉظ಺แදه

    •Python 3.5 Ͱ async/await ߏจ asyncioはこわくない こわい •ฒߦॲཧͷ ίʔυΛॻͨ͘ΊͷϥΠϒϥϦ •γϯάϧεϨουͰΠϕϯτϧʔϓͰඇಉظ IO •ઌ೔RFCͰඪ४Խ͞ΕͨWebن֨ QUIC ͷϦϑΝϨϯεత࣮૷ aioquic γϯάϧεϨου ฒߦॲཧ ඇಉظ IO
  29. 注⽂を取る - 料理を作る - 料理を運ぶ 注⽂を取る - 料理を作る - 料理を運ぶ

    注⽂を取る - 料理を作る - 料理を運ぶ asyncio コルーチンやタスク
  30. asyncio 注⽂を取る - 料理を作る - 料理を運ぶ 注⽂を取る - 料理を作る -

    料理を運ぶ 注⽂を取る - 料理を作る - 料理を運ぶ コルーチンやタスク
  31. イベントループを作る async def order(): print(“͝஫จ͸ʁ”) menu = await long_thinking() print(menu)

    async def long_thinking(): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ͹”
  32. イベントループを作る async def order(): print(“͝஫จ͸ʁ”) menu = await long_thinking() print(menu)

    async def long_thinking(): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ͹” asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ
  33. イベントループを作る async def order(): print(“͝஫จ͸ʁ”) menu = await long_thinking() print(menu)

    async def long_thinking(): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ͹” asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ ͝஫จ͸ʁ ͪΐͬͱ଴ͬͯͶ
  34. イベントループを作る async def order(): print(“͝஫จ͸ʁ”) menu = await long_thinking() print(menu)

    async def long_thinking(): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ͹” asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ ͝஫จ͸ʁ ͪΐͬͱ଴ͬͯͶ # …௕ߟͷͷͪ 😴
  35. イベントループを作る async def order(): print(“͝஫จ͸ʁ”) menu = await long_thinking() print(menu)

    async def long_thinking(): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ͹” asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ ͝஫จ͸ʁ ͪΐͬͱ଴ͬͯͶ ͟Δͦ͹ # ίϧʔνϯͷ໭ΓΛ ड͚औͬͯprint # …௕ߟͷͷͪ 😴
  36. イベントループを作る async def order(): print(“͝஫จ͸ʁ”) menu = await long_thinking() print(menu)

    async def long_thinking(): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ͹” asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ ͝஫จ͸ʁ ͪΐͬͱ଴ͬͯͶ ͟Δͦ͹ # ίϧʔνϯͷ໭ΓΛ ड͚औͬͯprint # …௕ߟͷͷͪ 😴
  37. イベントループを作る async def order(): print(“͝஫จ͸ʁ”) menu = await long_thinking() print(menu)

    async def long_thinking(): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ͹” asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ
  38. イベントループを作る async def order(): print(“͝஫จ͸ʁ”) menu = await long_thinking() print(menu)

    async def long_thinking(): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ͹” asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ
  39. イベントループを作る async def order(): print(“͝஫จ͸ʁ”) menu = await long_thinking() print(menu)

    async def long_thinking(): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ͹” asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ
  40. イベントループを作る def order(): print(“͝஫จ͸ʁ”) menu = long_thinking() print(menu) def long_thinking():

    print(“ͪΐͬͱ଴ͬͯͶ”) sleep(10000) return “͟Δͦ͹” order() # ࣮ߦ͢Δ
  41. イベントループを作る async def order(): print(“͝஫จ͸ʁ”) menu = await long_thinking() print(menu)

    async def long_thinking(): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ͹” asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ
  42. async def long_thinking(): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep() return “͟Δͦ͹” await の動作を確認する

    async def order(): print(“͝஫จ͸ʁ”) menu = await long_thinking() print([menu])
  43. async def long_thinking(num: int): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ͹”

    await の動作を確認する async def order(): print(“͝஫จ͸ʁ”) menu = await long_thinking() print([menu])
  44. await の動作を確認する async def order(): print(“͝஫จ͸ʁ”) menu = await long_thinking()

    print([menu]) async def long_thinking(num: int): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ͹”
  45. async def long_thinking(num: int): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ͹”

    async def order(): print(“͝஫จ͸ʁ”) menu1 = await long_thinking(1) menu2 = await long_thinking(2) menu3 = await long_thinking(3) print([menu1, memu2, menu3]) await の動作を確認する
  46. async def long_thinking(num: int): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ͹”

    async def order(): print(“͝஫จ͸ʁ”) menu1 = await long_thinking(1) menu2 = await long_thinking(2) menu3 = await long_thinking(3) print([menu1, memu2, menu3]) await の動作を確認する 並⾏実⾏すると3秒で終わるはず asyncio.run(order())
  47. async def long_thinking(num: int): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ͹”

    async def order(): print(“͝஫จ͸ʁ”) menu1 = await long_thinking(1) menu2 = await long_thinking(2) menu3 = await long_thinking(3) print([menu1, memu2, menu3]) await の動作を確認する 並⾏実⾏すると3秒で終わるはず asyncio.run(order()) l͝஫จ͸ʁz ͪΐͬͱ଴ͬͯͶ ͪΐͬͱ଴ͬͯͶ ͪΐͬͱ଴ͬͯͶ <  ͟Δͦ͹   ͟Δͦ͹   ͟Δͦ͹ > UJNF
  48. async def long_thinking(num: int): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ͹”

    async def order(): print(“͝஫จ͸ʁ”) menu1 = await long_thinking(1) print(menu1) menu2 = await long_thinking(2) print(menu2) menu3 = await long_thinking(3) print(menu3) await の動作を確認する 並⾏実⾏すると3秒で終わるはず asyncio.run(order())
  49. await の動作を確認する 並⾏実⾏すると3秒で終わるはず asyncio.run(order()) l͝஫จ͸ʁz ͪΐͬͱ଴ͬͯͶ  ͟Δͦ͹  ͪΐͬͱ଴ͬͯͶ

     ͟Δͦ͹  ͪΐͬͱ଴ͬͯͶ  b͟Δͦ͹  UJNF async def long_thinking(num: int): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ͹” async def order(): print(“͝஫จ͸ʁ”) menu1 = await long_thinking(1) print(menu1) menu2 = await long_thinking(2) print(menu2) menu3 = await long_thinking(3) print(menu3)
  50. await の動作を確認する 並⾏実⾏すると3秒で終わるはず asyncio.run(order()) コルーチンをawaitするだけでは 並⾏にならない 1つのコルーチンが終わってから 次のコルーチン 普通の関数実⾏ と同じ

    l͝஫จ͸ʁz ͪΐͬͱ଴ͬͯͶ  ͟Δͦ͹  ͪΐͬͱ଴ͬͯͶ  ͟Δͦ͹  ͪΐͬͱ଴ͬͯͶ  b͟Δͦ͹  UJNF async def long_thinking(num: int): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ͹” async def order(): print(“͝஫จ͸ʁ”) menu1 = await long_thinking(1) print(menu1) menu2 = await long_thinking(2) print(menu2) menu3 = await long_thinking(3) print(menu3)
  51. 初めてのタスク async def long_thinking(num: int): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(num) return num,

    “͟Δͦ͹” async def order(): print(“͝஫จ͸ʁ”) menu1 = await long_thinking(1) menu2 = await long_thinking(2) menu3 = await long_thinking(3) print([menu1, memu2, menu3]) asyncio.run(order())
  52. 初めてのタスク asyncio.run(order()) async def order(): print(“͝஫จ͸ʁ”) menu1 = await long_thinking(1)

    menu2 = await long_thinking(2) menu3 = await long_thinking(3) print([menu1, memu2, menu3]) async def long_thinking(num: int): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ͹”
  53. 初めてのタスク async def long_thinking(num: int): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(num) return num,

    “͟Δͦ͹” async def order(): print(“͝஫จ͸ʁ”) task1 = asyncio.create_task(long_thinking(1)) task2 = asyncio.create_task(long_thinking(2)) task3 = asyncio.create_task(long_thinking(3)) menu1 = await task1 menu2 = await task2 menu3 = await task3 print([menu1, memu2, menu3]) asyncio.run(order())
  54. 初めてのタスク async def order(): print(“͝஫จ͸ʁ”) task1 = asyncio.create_task(long_thinking(1)) task2 =

    asyncio.create_task(long_thinking(2)) task3 = asyncio.create_task(long_thinking(3)) menu1 = await task1 menu2 = await task2 menu3 = await task3 print([menu1, memu2, menu3]) asyncio.run(order()) 実⾏してみましょう async def long_thinking(num: int): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ͹”
  55. 初めてのタスク async def customers_long_thinking_order(num: int): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(num) return num,

    “͟Δͦ͹” async def order(): print(“͝஫จ͸ʁ”) task1 = asyncio.create_task(long_thinking(1)) task2 = asyncio.create_task(long_thinking(2)) task3 = asyncio.create_task(long_thinking(3)) menu1 = await task1 menu2 = await task2 menu3 = await task3 print([menu1, memu2, menu3]) asyncio.run(order()) 実⾏してみましょう ͪΐͬͱ଴ͬͯͶ ͪΐͬͱ଴ͬͯͶ ͪΐͬͱ଴ͬͯͶ <  ͟Δͦ͹   ͟Δͦ͹   ͟Δͦ͹ > UJNF
  56. 初めてのタスク async def long_thinking(num: int): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(num) return num,

    “͟Δͦ͹” async def order(): print(“͝஫จ͸ʁ”) task1 = asyncio.create_task(long_thinking(1)) task2 = asyncio.create_task(long_thinking(2)) task3 = asyncio.create_task(long_thinking(3)) menu1 = await task1 print(menu1) menu2 = await task2 print(menu2) menu3 = await task3 print(menu2) asyncio.run(order()) 実⾏してみましょう
  57. 初めてのタスク async def order(): print(“͝஫จ͸ʁ”) task1 = asyncio.create_task(long_thinking(1)) task2 =

    asyncio.create_task(long_thinking(2)) task3 = asyncio.create_task(long_thinking(3)) menu1 = await task1 print(menu1) menu2 = await task2 print(menu2) menu3 = await task3 print(menu2) asyncio.run(order()) 実⾏してみましょう l͝஫จ͸ʁz ͪΐͬͱ଴ͬͯͶ ͪΐͬͱ଴ͬͯͶ ͪΐͬͱ଴ͬͯͶ  ͟Δͦ͹   ͟Δͦ͹   ͟Δͦ͹  UJNF async def long_thinking(num: int): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ͹”
  58. 初めてのタスク async def order(): print(“͝஫จ͸ʁ”) task1 = asyncio.create_task(long_thinking(1)) task2 =

    asyncio.create_task(long_thinking(2)) task3 = asyncio.create_task(long_thinking(3)) menu1 = await task1 print(menu1) menu2 = await task2 print(menu2) menu3 = await task3 print(menu2) asyncio.run(order()) 実⾏してみましょう l͝஫จ͸ʁz ͪΐͬͱ଴ͬͯͶ ͪΐͬͱ଴ͬͯͶ ͪΐͬͱ଴ͬͯͶ  ͟Δͦ͹   ͟Δͦ͹   ͟Δͦ͹  UJNF async def long_thinking(num: int): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ͹”
  59. gatherを利⽤して結果をまとめて受け取る async def long_thinking(num: int): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(num) return num,

    “͟Δͦ͹” async def order(): print(“͝஫จ͸ʁ”) tasks = [ long_thinking(3000), long_thinking(200), long_thinking(10), ] result = await asyncio.gather(*tasks) print(result) asyncio.run(order())
  60. async def order(): print(“͝஫จ͸ʁ”) tasks = [ long_thinking(3000), long_thinking(200), long_thinking(10),

    ] result = await asyncio.gather(*tasks) print(result) gatherを利⽤して結果をまとめて受け取る async def long_thinking(num: int): print(“ͪΐͬͱ଴ͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ͹” asyncio.run(order()) l͝஫จ͸ʁz ͪΐͬͱ଴ͬͯͶ ͪΐͬͱ଴ͬͯͶ ͪΐͬͱ଴ͬͯͶ <  ͟Δͦ͹   ͟Δͦ͹   ͟Δͦ͹ > UJNF
  61. ίϧʔνϯ asyncioはこわくない λεΫ ίϧʔνϯΛΠϕϯτϧʔϓʹొ࿥ɻ ྑ͖ʹܭΒͬͯ੾Γସ͑ͯ͘ΕΔɻฒߦॲཧͯ͘͠ΕΔɻ ؔ਺ʹ async Λ͚ͭΔ͚ͩ ؊͸await async

    def await λεΫ ·ͨ͸ ίϧʔνϯ create_task(ίϧʔνϯ) - Πϕϯτϧʔϓʹొ࿥ gather(λεΫ ·ͨ͸ ίϧʔνϯ) - ·ͱΊͯ݁ՌΛड͚औΕΔ
  62. 考え中のお客様がgoogle検索 async def long_think(num: int): print("ͪΐͬͱ଴ͬͯͶ") response = requests.get("https://www.google.com/search?q=͓͍͍͓͠ڶഴ͸ʁ") return

    num, response, "͟Δͦ͹" async def order(): print("“͝஫จ͸ʁ”") tasks = [long_think(3), long_think(2), long_think(1)] result = await asyncio.gather(*tasks) print(result) start = time() asyncio.run(order()) print("time: ", time() - start)
  63. 考え中のお客様がgoogle検索 async def long_think(num: int): print("ͪΐͬͱ଴ͬͯͶ") response = requests.get("https://www.google.com/search?q=͓͍͍͓͠ڶഴ͸ʁ") return

    num, response, "͟Δͦ͹" async def order(): print("“͝஫จ͸ʁ”") tasks = [long_think(3), long_think(2), long_think(1)] result = await asyncio.gather(*tasks) print(result) start = time() asyncio.run(order()) print("time: ", time() - start)
  64. 考え中のお客様がgoogle検索 async def long_think(num: int): print("ͪΐͬͱ଴ͬͯͶ") response = requests.get("https://www.google.com/search?q=͓͍͍͓͠ڶഴ͸ʁ") return

    num, response, "͟Δͦ͹" async def order(): print("“͝஫จ͸ʁ”") tasks = [long_think(3), long_think(2), long_think(1)] result = await asyncio.gather(*tasks) print(result) start = time() asyncio.run(order()) print("time: ", time() - start)
  65. 考え中のお客様がgoogle検索 async def long_think(num: int): print("ͪΐͬͱ଴ͬͯͶ") response = requests.get("https://www.google.com/search?q=͓͍͍͓͠ڶഴ͸ʁ") return

    num, response, "͟Δͦ͹" async def order(): print("“͝஫จ͸ʁ”") tasks = [long_think(3), long_think(2), long_think(1)] result = await asyncio.gather(*tasks) print(result) start = time() asyncio.run(order()) print("time: ", time() - start) l͝஫จ͸ʁz ͪΐͬͱ଴ͬͯͶ ͪΐͬͱ଴ͬͯͶ ͪΐͬͱ଴ͬͯͶ <  3FTQPOTF<> b͟Δͦ͹` > UJNF
  66. 考え中のお客様がgoogle検索 async def long_think(num: int): print("ͪΐͬͱ଴ͬͯͶ") response = requests.get("https://www.google.com/search?q=͓͍͍͓͠ڶഴ͸ʁ") print(“ܾ·ͬͨʂ")

    return num, response, "͟Δͦ͹" async def order(): print("“͝஫จ͸ʁ”") tasks = [long_think(3), long_think(2), long_think(1)] result = await asyncio.gather(*tasks) print(result) start = time() asyncio.run(order()) print("time: ", time() - start)
  67. 考え中のお客様がgoogle検索 async def long_think(num: int): print("ͪΐͬͱ଴ͬͯͶ") response = requests.get("https://www.google.com/search?q=͓͍͍͓͠ڶഴ͸ʁ") print(“ܾ·ͬͨʂ")

    return num, response, "͟Δͦ͹" async def order(): print("“͝஫จ͸ʁ”") tasks = [long_think(3), long_think(2), long_think(1)] result = await asyncio.gather(*tasks) print(result) start = time() asyncio.run(order()) print("time: ", time() - start) l͝஫จ͸ʁz ͪΐͬͱ଴ͬͯͶ ܾ·ͬͨʂ ͪΐͬͱ଴ͬͯͶ ܾ·ͬͨʂ ͪΐͬͱ଴ͬͯͶ ܾ·ͬͨʂ <  3FTQPOTF<> b͟Δͦ͹` > UJNF
  68. 考え中のお客様がgoogle検索 async def long_think(num: int): print("ͪΐͬͱ଴ͬͯͶ") response = requests.get("https://www.google.com/search?q=͓͍͍͓͠ڶഴ͸ʁ") return

    num, response, "͟Δͦ͹" async def order(): print("“͝஫จ͸ʁ”") tasks = [long_think(3), long_think(2), long_think(1)] result = await asyncio.gather(*tasks) print(result) start = time() asyncio.run(order(), debug=True) print("time: ", time() - start)
  69. 考え中のお客様がgoogle検索 async def long_think(num: int): print("ͪΐͬͱ଴ͬͯͶ") response = requests.get("https://www.google.com/search?q=͓͍͍͓͠ڶഴ͸ʁ") return

    num, response, "͟Δͦ͹" async def order(): print("“͝஫จ͸ʁ”") tasks = [long_think(3), long_think(2), long_think(1)] result = await asyncio.gather(*tasks) print(result) start = time() asyncio.run(order(), debug=True) print("time: ", time() - start) l͝஫จ͸ʁz ͪΐͬͱ଴ͬͯͶ UPPLTFDPOET ͪΐͬͱ଴ͬͯͶ UPPLTFDPOET ͪΐͬͱ଴ͬͯͶ UPPLTFDPOET  3FTQPOTF<> ͟Δͦ͹ > UJNF
  70. 考え中のお客様がgoogle検索 async def long_think(client, num: int): print("ͪΐͬͱ଴ͬͯͶ") response = await

    client.get("https://www.google.com/search?q=͓͍͍͓͠ڶഴ͸ʁ") return num, response, "͟Δͦ͹" async def order(): print("“͝஫จ͸ʁ”") async with httpx.AsyncClient() as client: tasks = [long_think(client, 1), long_think(client, 2), long_think(client, 3)] result = await asyncio.gather(*tasks) print(result) start = time() asyncio.run(order(), debug=True) print("time: ", time() - start)
  71. 考え中のお客様がgoogle検索 async def long_think(client, num: int): print("ͪΐͬͱ଴ͬͯͶ") response = await

    client.get("https://www.google.com/search?q=͓͍͍͓͠ڶഴ͸ʁ") return num, response, "͟Δͦ͹" async def order(): print("“͝஫จ͸ʁ”") async with httpx.AsyncClient() as client: tasks = [long_think(client, 1), long_think(client, 2), long_think(client, 3)] result = await asyncio.gather(*tasks) print(result) start = time() asyncio.run(order(), debug=True) print("time: ", time() - start) l͝஫จ͸ʁz ͪΐͬͱ଴ͬͯͶ ͪΐͬͱ଴ͬͯͶ ͪΐͬͱ଴ͬͯͶ <  3FTQPOTF<0,> b͟Δͦ͹`  UJNF
  72. ίϧʔνϯ asyncioはこわくない λεΫ Πϕϯτϧʔϓ create_task(ίϧʔνϯ) - Πϕϯτϧʔϓʹొ࿥ gather(λεΫ ·ͨ͸ ίϧʔνϯ)

    - ·ͱΊͯ݁ՌΛड͚औΕΔ async def await λεΫ ·ͨ͸ ίϧʔνϯ asynio.run(ίϧʔνϯ)
  73. ίϧʔνϯ asyncioはこわくない λεΫ Πϕϯτϧʔϓ create_task(ίϧʔνϯ) - Πϕϯτϧʔϓʹొ࿥ gather(λεΫ ·ͨ͸ ίϧʔνϯ)

    - ·ͱΊͯ݁ՌΛड͚औΕΔ async def await λεΫ ·ͨ͸ ίϧʔνϯ asynio.run(ίϧʔνϯ) まずはこれを抑えましょう
  74. asyncioはこわくない 緩やかな根拠 Python 3.7 ͷΞοϓσʔτ asyncio.create_task(), asyncio.gather(), asyncio.run() ͷ௥Ճ python

    core developer Yury Selivanov ʢϢʔϦʔηϦόϊϑʣࢯ ͷొஃ PEP 492 -- Coroutines with async and await syntax ஶऀ europython 2018 - Asyncio in Python 3 7 and 3 8
  75. asyncioはこわくない 緩やかな根拠 Python 3.7 ͷΞοϓσʔτ asyncio.create_task(), asyncio.gather(), asyncio.run() ͷ௥Ճ Yury

    Selivanov ʢϢʔϦʔηϦόϊϑʣࢯ PEP 492 -- Coroutines with async and await syntax ஶऀ
  76. asyncioはこわくない 緩やかな根拠 Python 3.7 ͷΞοϓσʔτ asyncio.create_task(), asyncio.gather(), asyncio.run() ͷ௥Ճ Yury

    Selivanov ʢϢʔϦʔηϦόϊϑʣࢯ PEP 492 -- Coroutines with async and await syntax ஶऀ normal ϋʔυίΞ
  77. asyncioはこわくない 緩やかな根拠 Python 3.7 ͷΞοϓσʔτ asyncio.create_task(), asyncio.gather(), asyncio.run() ͷ௥Ճ Yury

    Selivanov ʢϢʔϦʔηϦόϊϑʣࢯ PEP 492 -- Coroutines with async and await syntax ஶऀ
  78. asyncioはこわくない 緩やかな根拠 Python 3.7 ͷΞοϓσʔτ asyncio.create_task(), asyncio.gather(), asyncio.run() ͷ௥Ճ python

    core developer Yury Selivanov ʢϢʔϦʔηϦόϊϑʣࢯ ͷొஃ PEP 492 -- Coroutines with async and await syntax ஶऀ europython 2018 - Asyncio in Python 3 7 and 3 8
  79. asyncioはこわくない 緩やかな根拠 Python 3.7 ͷΞοϓσʔτ asyncio.create_task(), asyncio.gather(), asyncio.run() ͷ௥Ճ python

    core developer Yury Selivanov ʢϢʔϦʔηϦόϊϑʣࢯ ͷొஃ PEP 492 -- Coroutines with async and await syntax ஶऀ europython 2018 - Asyncio in Python 3 7 and 3 8 Python 3.10 ߴϨϕϧAPI(gather/wait/sleep … ) ͷҾ਺ʹloopΦϒδΣΫτͷࢦఆ͕ෆՄ
  80. asyncioはこわくない 緩やかな根拠 Python 3.7 ͷΞοϓσʔτ asyncio.create_task(), asyncio.gather(), asyncio.run() ͷ௥Ճ python

    core developer Yury Selivanov ʢϢʔϦʔηϦόϊϑʣࢯ ͷొஃ PEP 492 -- Coroutines with async and await syntax ஶऀ europython 2018 - Asyncio in Python 3 7 and 3 8 Python 3.10 ߴϨϕϧAPI(gather/wait/sleep … ) ͷҾ਺ʹloopΦϒδΣΫτͷࢦఆ͕ෆՄ loopΦϒδΣΫτ΍futureΦϒδΣΫτΛҙࣝͤͣʹasyncioར༻Մೳʹ
  81. asyncioはこわくない 緩やかな根拠 Python 3.7 ͷΞοϓσʔτ asyncio.create_task(), asyncio.gather(), asyncio.run() ͷ௥Ճ python

    core developer Yury Selivanov ʢϢʔϦʔηϦόϊϑʣࢯ ͷొஃ PEP 492 -- Coroutines with async and await syntax ஶऀ europython 2018 - Asyncio in Python 3 7 and 3 8 Python 3.10 ߴϨϕϧAPI(gather/wait/sleep … ) ͷҾ਺ʹloopΦϒδΣΫτͷࢦఆ͕ෆՄ loopΦϒδΣΫτ΍futureΦϒδΣΫτΛҙࣝͤͣʹasyncioར༻Մೳʹ Ξοϓσʔτ͕଎͍
  82. asyncioはこわくない 緩やかな根拠 Python 3.7 ͷΞοϓσʔτ asyncio.create_task(), asyncio.gather(), asyncio.run() ͷ௥Ճ python

    core developer Yury Selivanov ʢϢʔϦʔηϦόϊϑʣࢯ ͷొஃ PEP 492 -- Coroutines with async and await syntax ஶऀ europython 2018 - Asyncio in Python 3 7 and 3 8 Python 3.10 ߴϨϕϧAPI(gather/wait/sleep … ) ͷҾ਺ʹloopΦϒδΣΫτͷࢦఆ͕ෆՄ loopΦϒδΣΫτ΍futureΦϒδΣΫτΛҙࣝͤͣʹasyncioར༻Մೳʹ Ξοϓσʔτ͕଎͍ ༗༻ͳαϯϓϧίʔυ΋աڈͷόʔδϣϯΛϕʔεʹ͍ͯ͠Δ΋ͷ΋ଟ͍
  83. ίϧʔνϯ asyncioはこわくない λεΫ Πϕϯτϧʔϓ create_task(ίϧʔνϯ) - Πϕϯτϧʔϓʹొ࿥ gather(λεΫ ·ͨ͸ ίϧʔνϯ)

    - ·ͱΊͯ݁ՌΛड͚औΕΔ async def await λεΫ ·ͨ͸ ίϧʔνϯ asynio.run(ίϧʔνϯ) まずはこれを抑えましょう
  84. コ asyncio ちょっとわがまま 外部IOの発⽣する処理 HTTPアクセス(APIとか) データベースアクセス urllib.requests requests psycopg2 mysqlclient

    aiohttpやhttpx asyncpg "XFTPNF IUUQTHJUIVCDPNUJNPGVSSFSBXFTPNFBTZODJP BJPMJCT IUUQTHJUIVCDPNBJPMJCT FODPEF IUUQTXXXFODPEFJPQSPKFDUT aiomysql
  85. コ asyncio ちょっとわがまま 外部IOの発⽣する処理 HTTPアクセス(APIとか) データベースアクセス urllib.requests requests psycopg2 mysqlclient

    aiohttpやhttpx asyncpg aiomysql "XFTPNF IUUQTHJUIVCDPNUJNPGVSSFSBXFTPNFBTZODJP BJPMJCT IUUQTHJUIVCDPNBJPMJCT FODPEF IUUQTXXXFODPEFJPQSPKFDUT
  86. asyncpg の簡単な使いかた # ίωΫγϣϯϓʔϧΛऔಘ async def main(): async with asyncpg.create_pool(dns)

    as pool: await pool.execute(...) dns = "postgresql://user:pass@host:5432/database" import asyncpg
  87. asyncpg の簡単な使いかた # ίωΫγϣϯϓʔϧΛऔಘ async def main(): async with asyncpg.create_pool(dns)

    as pool: await pool.execute(...) dns = "postgresql://user:pass@host:5432/database" import asyncpg https://magicstack.github.io/asyncpg/
  88. asyncpg の簡単な使いかた # ෳ਺ͷΫΤϦΛ࣮ߦ͢Δ৔߹ acquireϝιουͰίωΫγϣϯΛऔಘ async with asyncpg.create_pool(dns) as pool:

    async with pool.acquire() as conn: result1 = await conn.execute("INSERT …") result2 = await conn.fetch("SELECT 1") import asyncpg async def main(): async with asyncpg.create_pool(dns) as pool: await pool.execute(...) dns = "postgresql://user:pass@host:5432/database"
  89. asyncpg の簡単な使いかた async def main(): async with asyncpg.create_pool(dns) as pool:

    tasks = [ pool.fetchrow("SELECT * FROM users WHERE id = $1", 1), pool.fetchrow("SELECT * FROM users WHERE id = $1", 2), pool.fetchrow("SELECT * FROM users WHERE id = $1", 3), ] result = await asyncio.gather(*tasks) print(result)
  90. import asyncpg dns = "postgresql:// user:pass@host:5432/database" class Database: async def

    create_pool(self): self.pool = await asyncpg.create_pool(dsn=dns) asyncpg と FastAPI
  91. asyncpg と FastAPI app = FastAPI() db = Database() @app.on_event("startup")

    async def startup(): await db.create_pool() @app.on_event("shutdown") async def shutdown(): # cleanup pass from fastapi import FastAPI, Request
  92. asyncpg と FastAPI @app.middleware("http") async def db_session_middleware(request: Request, call_next): request.state.pgpool

    = db.pool response = await call_next(request) return response @app.get("/") async def sample(request: Request): print(request.state.pgpool)
  93. asyncpg と FastAPI async def get_rows(conn, num: int): """औಘ""" print(“ͪΐͬͱ଴ͬͯͶ

    {num=}") result = await conn.fetchrow("SELECT * FROM users WHERE id = $1", num) print(“ܾ·ͬͨʂ”) return result
  94. asyncpg と FastAPI @app.get("/fetch") async def fetch_many(request: Request): tasks =

    [get_rows(request.state.pgpool, number) for number in range(0, 100)] return await asyncio.gather(*tasks, return_exceptions=False) async def get_rows(conn, num: int): """औಘ""" print(“ͪΐͬͱ଴ͬͯͶ {num=}") result = await conn.fetchrow("SELECT * FROM users WHERE id = $1", num) print(“ܾ·ͬͨʂ”) return result
  95. asyncpg と FastAPI async def get_rows(conn, num: int): """औಘ""" print(“ͪΐͬͱ଴ͬͯͶ

    {num=}") result = await conn.fetchrow("SELECT * FROM users WHERE id = $1", num) print(“ܾ·ͬͨʂ”) return result
  96. asyncpg と FastAPI INFO: Application startup complete. ͪΐͬͱ଴ͬͯͶ num=0 ͪΐͬͱ଴ͬͯͶ

    num=1 ͪΐͬͱ଴ͬͯͶ num=2 ͪΐͬͱ଴ͬͯͶ num=3 ͪΐͬͱ଴ͬͯͶ num=4 ... async def get_rows(conn, num: int): """औಘ""" print(“ͪΐͬͱ଴ͬͯͶ {num=}") result = await conn.fetchrow("SELECT * FROM users WHERE id = $1", num) print(“ܾ·ͬͨʂ”) return result
  97. asyncpg と FastAPI INFO: Application startup complete. ͪΐͬͱ଴ͬͯͶ num=0 ͪΐͬͱ଴ͬͯͶ

    num=1 ͪΐͬͱ଴ͬͯͶ num=2 ͪΐͬͱ଴ͬͯͶ num=3 ͪΐͬͱ଴ͬͯͶ num=4 ... async def get_rows(conn, num: int): """औಘ""" print(“ͪΐͬͱ଴ͬͯͶ {num=}") result = await conn.fetchrow("SELECT * FROM users WHERE id = $1", num) print(“ܾ·ͬͨʂ”) return result ... ܾ·ͬͨʂ ܾ·ͬͨʂ ܾ·ͬͨʂ ܾ·ͬͨʂ INFO: 127.0.0.1:50033 - "GET /fetch INFO: 127.0.0.1:50038 - "GET /docs INFO: 127.0.0.1:50038 - "GET /opena
  98. •ίϧʔνϯ͸ා͘ͳ͍ - Minimum Viable Programmer https://www.rhoboro.com/2019/02/09/coroutine-abstract.html •Using Asyncio in Python

    - Oreilly & Associates Inc 謝辞 •Yury Selivanov - Asyncio in Python 3 7 and 3 8 https://www.youtube.com/watch?v=ReXxO_azV-w •asyncioͷTaskʹؔ͢Δجૅ஌ࣝ https://aish.dev/python/20200711_asyncio_task.html •To many many internet articles, great books and member. 🙏