Share
Embed
Transcript
308 slides
-
•ా ൏ʢJunya Fukudaʣʢ@JunyaFffʣ •גࣜձࣾຊγεςϜٕݚʢJSLʣॴଐ ݝͷձࣾ •WebΤϯδχΞ •GEEKLAB.NAGANO - ίϛϡχςΟεϖʔεӡӦ •Effective
Python ͷಡॻձͬͯ·͢ 📚 •ʢ͓͠͝ͱͰʣόϦϡʔϒοΫε •ຊ͖ʹѪ͞ΕΔαʔϏεΛࢦͯ͠ʢݹຊങऔɾൢചʣ ΪʔΫϥϘಡॻձ •GEEKLAB.NAGANO - ίϛϡχςΟεϖʔεӡӦ -
って 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 -
って •Fast: Very high performance, on par with NodeJS and
Go ʢߴ: NodeJS Go ฒΈͷͱͯߴ͍ύϑΥʔϚϯεʣ https://fastapi.tiangolo.com/ •ߴύϑΥʔϚϯε -
って •Fast: Very high performance, on par with NodeJS and
Go ʢߴ: NodeJS Go ฒΈͷͱͯߴ͍ύϑΥʔϚϯεʣ https://fastapi.tiangolo.com/ •ߴύϑΥʔϚϯε •͍Ζ͍Ζͳ͕֯͋Δ -
って •Fast: Very high performance, on par with NodeJS and
Go ʢߴ: NodeJS Go ฒΈͷͱͯߴ͍ύϑΥʔϚϯεʣ https://fastapi.tiangolo.com/ •ߴύϑΥʔϚϯε •͍Ζ͍Ζͳ͕֯͋Δ - ڞ௨͢Δͷඇಉظϓϩάϥϛϯά -
って •Fast: Very high performance, on par with NodeJS and
Go ʢߴ: NodeJS Go ฒΈͷͱͯߴ͍ύϑΥʔϚϯεʣ https://fastapi.tiangolo.com/ •ߴύϑΥʔϚϯε •͍Ζ͍Ζͳ͕֯͋Δ - ڞ௨͢Δͷඇಉظϓϩάϥϛϯά •FastAPIͬͯඇಉظʁ -
って •Fast: Very high performance, on par with NodeJS and
Go ʢߴ: NodeJS Go ฒΈͷͱͯߴ͍ύϑΥʔϚϯεʣ https://fastapi.tiangolo.com/ •ߴύϑΥʔϚϯε •͍Ζ͍Ζͳ͕֯͋Δ - ڞ௨͢Δͷඇಉظϓϩάϥϛϯά •FastAPIͬͯඇಉظʁ •Webͷ෦ɺStarlette -
って •Fast: Very high performance, on par with NodeJS and
Go ʢߴ: NodeJS Go ฒΈͷͱͯߴ͍ύϑΥʔϚϯεʣ https://fastapi.tiangolo.com/ •ߴύϑΥʔϚϯε •͍Ζ͍Ζͳ͕֯͋Δ - ڞ௨͢Δͷඇಉظϓϩάϥϛϯά •FastAPIͬͯඇಉظʁ •Webͷ෦ɺStarlette = ASGIରԠͷWeb FW ASGIରԠ -
•WebαʔόͱϑϨʔϜϫʔΫΛͭͳ͙ɺ༷ •WSGIͷਫ਼ਆతޙܧ •WSGI って? ASGIରԠ •αʔόɿGunicorn uWSGI ⁶ ϑϨʔϜϫʔΫɿDjango, Flask,
Pyramid •ASGI •αʔόɿUvicorn, Hypercorn ϑϨʔϜϫʔΫɿDjango, FastAPI, Starlette -
•WebαʔόͱϑϨʔϜϫʔΫΛͭͳ͙ɺ༷ •WSGIͷਫ਼ਆతޙܧ •WSGI って? ASGIରԠ •αʔόɿGunicorn uWSGI ⁶ ϑϨʔϜϫʔΫɿDjango, Flask,
Pyramid •ASGI •αʔόɿUvicorn, Hypercorn ⁶ ϑϨʔϜϫʔΫɿDjango, FastAPI, Starlette -
•WebαʔόͱϑϨʔϜϫʔΫΛͭͳ͙ɺ༷ •WSGIͷਫ਼ਆతޙܧ •WSGI って? ASGIରԠ •αʔόɿGunicorn uWSGI ⁶ ϑϨʔϜϫʔΫɿDjango, Flask,
Pyramid •ASGI •αʔόɿUvicorn, Hypercorn ⁶ ϑϨʔϜϫʔΫɿDjango, FastAPI, Starlette -
•WebαʔόͱϑϨʔϜϫʔΫΛͭͳ͙ɺ༷ •WSGIͷਫ਼ਆతޙܧ •WSGI って? ASGIରԠ •αʔόɿGunicorn uWSGI ⁶ ϑϨʔϜϫʔΫɿDjango, Flask,
Pyramid •ASGI •αʔόɿUvicorn, Hypercorn ⁶ ϑϨʔϜϫʔΫɿDjango, FastAPI, Starlette •WSGIͱASGIɺͳʹ͕ҧ͏ͷ͔ -
ASGI対応 •WebαʔόͱϑϨʔϜϫʔΫΛͭͳ͙ɺ༷ •WSGIͷਫ਼ਆతޙܧ •Gunicorn uWSGI ⁶ Django Flask,
Pyramid •Uvicorn Hypercorn ⁶ Django FastAPI, Starlette って? asyncioに対応したインターフェース仕様 -
•Python 3.4 Ͱ Ճ͞Εͨඪ४ϥΠϒϥϦ •Python 3.6 Ͱ ඇಉظδΣωϨʔλʔ / ඇಉظแදه
•Python 3.5 Ͱ async/await ߏจ asyncioはこわくない -
•Python 3.4 Ͱ Ճ͞Εͨඪ४ϥΠϒϥϦ •Python 3.6 Ͱ ඇಉظδΣωϨʔλʔ / ඇಉظแදه
•Python 3.5 Ͱ async/await ߏจ asyncioはこわくない •ฒߦॲཧͷ ίʔυΛॻͨ͘ΊͷϥΠϒϥϦ -
•Python 3.4 Ͱ Ճ͞Εͨඪ४ϥΠϒϥϦ •Python 3.6 Ͱ ඇಉظδΣωϨʔλʔ / ඇಉظแදه
•Python 3.5 Ͱ async/await ߏจ asyncioはこわくない •ฒߦॲཧͷ ίʔυΛॻͨ͘ΊͷϥΠϒϥϦ •γϯάϧεϨουͰΠϕϯτϧʔϓͰඇಉظ IO -
•Python 3.4 Ͱ Ճ͞Εͨඪ४ϥΠϒϥϦ •Python 3.6 Ͱ ඇಉظδΣωϨʔλʔ / ඇಉظแදه
•Python 3.5 Ͱ async/await ߏจ asyncioはこわくない •ฒߦॲཧͷ ίʔυΛॻͨ͘ΊͷϥΠϒϥϦ •γϯάϧεϨουͰΠϕϯτϧʔϓͰඇಉظ IO •ࠓRFCͰඪ४Խ͞ΕͨWebن֨ QUIC ͷॳظͷϦϑΝϨϯεత࣮ aioquic -
•͜Θ͔ͬͨ •Using Asyncio in Python - ΦϥΠϦʔ ʢ🐸ຊʣ asyncio めっちゃこわい
•2ͭͷग़ձ͍ https://www.oreilly.com/library/view/using-asyncio-in/9781492075325/ -
•͜Θ͔ͬͨ •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/ -
•͜Θ͔ͬͨ •ίϧʔνϯා͘ͳ͍ - Minimum Viable Programmer ʢϒϩάʣ •Using Asyncio in
Python - ΦϥΠϦʔ ʢ🐸ຊʣ asyncio めっちゃこわい •2ͭͷग़ձ͍ -
•͜Θ͔ͬͨ •ίϧʔνϯා͘ͳ͍ - Minimum Viable Programmer ʢϒϩάʣ •Using Asyncio in
Python - ΦϥΠϦʔ ʢ🐸ຊʣ asyncio めっちゃこわい •2ͭͷग़ձ͍ •࣮ͰΔasyncio -Πϕϯτϧʔϓͷਖ਼ମͱ - REI SUYAMA ͞Μ •DAY2 - 10.16(SAT) 16:20 - #pyconjp_2 -
•͜Θ͔ͬͨ •ίϧʔνϯා͘ͳ͍ - Minimum Viable Programmer ʢϒϩάʣ •Using Asyncio in
Python - ΦϥΠϦʔ ʢ🐸ຊʣ asyncio めっちゃこわい •2ͭͷग़ձ͍ •࣮ͰΔasyncio -Πϕϯτϧʔϓͷਖ਼ମͱ - REI SUYAMA ͞Μ •DAY2 - 10.16(SAT) 16:20 - #pyconjp_2 -
•Python 3.4 Ͱ Ճ͞Εͨඪ४ϥΠϒϥϦ •Python 3.6 Ͱ ඇಉظδΣωϨʔλʔ / ඇಉظแදه
•Python 3.5 Ͱ async/await ߏจ asyncioはこわくない こわい •ฒߦॲཧͷ ίʔυΛॻͨ͘ΊͷϥΠϒϥϦ •γϯάϧεϨουͰΠϕϯτϧʔϓͰඇಉظ IO •ઌRFCͰඪ४Խ͞ΕͨWebن֨ QUIC ͷϦϑΝϨϯεత࣮ aioquic -
•Python 3.4 Ͱ Ճ͞Εͨඪ४ϥΠϒϥϦ •Python 3.6 Ͱ ඇಉظδΣωϨʔλʔ / ඇಉظแදه
•Python 3.5 Ͱ async/await ߏจ asyncioはこわくない こわい •ฒߦॲཧͷ ίʔυΛॻͨ͘ΊͷϥΠϒϥϦ •γϯάϧεϨουͰΠϕϯτϧʔϓͰඇಉظ IO •ઌRFCͰඪ४Խ͞ΕͨWebن֨ QUIC ͷϦϑΝϨϯεత࣮ aioquic -
•Python 3.4 Ͱ Ճ͞Εͨඪ४ϥΠϒϥϦ •Python 3.6 Ͱ ඇಉظδΣωϨʔλʔ / ඇಉظแදه
•Python 3.5 Ͱ async/await ߏจ asyncioはこわくない こわい •ฒߦॲཧͷ ίʔυΛॻͨ͘ΊͷϥΠϒϥϦ •γϯάϧεϨουͰΠϕϯτϧʔϓͰඇಉظ IO •ઌRFCͰඪ४Խ͞ΕͨWebن֨ QUIC ͷϦϑΝϨϯεత࣮ aioquic -
並⾏処理 注⽂を取る - 料理を作る - 料理を運ぶ 注⽂を取る - 料理を作る -
料理を運ぶ 注⽂を取る - 料理を作る - 料理を運ぶ -
並⾏処理 注⽂を取る - 料理を作る - 料理を運ぶ 注⽂を取る - 料理を作る -
料理を運ぶ 注⽂を取る - 料理を作る - 料理を運ぶ -
並⾏処理 注⽂を取る - 料理を作る - 料理を運ぶ 注⽂を取る - 料理を作る -
料理を運ぶ 注⽂を取る - 料理を作る - 料理を運ぶ asyncio シングルスレッド -
並⾏処理 注⽂を取る - 料理を作る - 料理を運ぶ 注⽂を取る - 料理を作る -
料理を運ぶ 注⽂を取る - 料理を作る - 料理を運ぶ asyncio シングルスレッド = ワンオペ -
並⾏処理 注⽂を取る - 料理を作る - 料理を運ぶ 注⽂を取る - 料理を作る -
料理を運ぶ 注⽂を取る - 料理を作る - 料理を運ぶ asyncio シングルスレッド = ワンオペ 同じ⼈ -
並⾏処理 注⽂を取る - 料理を作る - 料理を運ぶ 注⽂を取る - 料理を作る -
料理を運ぶ 注⽂を取る - 料理を作る - 料理を運ぶ asyncio シングルスレッド = ワンオペ どのタイミングでタスクの切り替えをするのでしょうか 🤔 -
asyncio シングルスレッド = ワンオペ どのタイミングでタスクの切り替えをするのでしょうか 🤔 あなた 考え中のお客様 ←注⽂待ち あなたから⾒て、
外部のお客様、から 注⽂という ⼊⼒を待つ状態 -
•Python 3.4 Ͱ Ճ͞Εͨඪ४ϥΠϒϥϦ •Python 3.6 Ͱ ඇಉظδΣωϨʔλʔ / ඇಉظแදه
•Python 3.5 Ͱ async/await ߏจ asyncioはこわくない こわい •ฒߦॲཧͷ ίʔυΛॻͨ͘ΊͷϥΠϒϥϦ •γϯάϧεϨουͰΠϕϯτϧʔϓͰඇಉظ IO •ઌRFCͰඪ४Խ͞ΕͨWebن֨ QUIC ͷϦϑΝϨϯεత࣮ aioquic γϯάϧεϨου ฒߦॲཧ ඇಉظ IO -
注⽂を取る - 料理を作る - 料理を運ぶ 注⽂を取る - 料理を作る - 料理を運ぶ
注⽂を取る - 料理を作る - 料理を運ぶ asyncio -
注⽂を取る - 料理を作る - 料理を運ぶ 注⽂を取る - 料理を作る - 料理を運ぶ
注⽂を取る - 料理を作る - 料理を運ぶ asyncio コルーチンやタスク -
asyncio 注⽂を取る - 料理を作る - 料理を運ぶ 注⽂を取る - 料理を作る -
料理を運ぶ 注⽂を取る - 料理を作る - 料理を運ぶ コルーチンやタスク -
•ίϧʔνϯͱɺϧʔνϯʢ͍ҙຯͰͷؔʣͷҰछ コルーチン •Co-routine ͱॻ͘ •ؔͷఆٛʹ async Λ͚ͭΔ ɺதஅͯ͠࠶։͢Δͱ͜Ζʹ await •
asyncio Λ͏্Ͱͷجຊͷ͖ •ॲཧΛ్தͰதஅͯ͠࠶։Ͱ͖Δϧʔνϯ -
async def customers_long_thinking_order(): asyncio.sleep(10000) return “͟Δͦ” 時間のかかる処理にawait await •࣌ؒͷ͔͔ΔॲཧʹawaitΛ͚ͭΔ •͔ͦ͜Βதஅͱ࠶։͢Δ
•await Λ͚ͭΔ͜ͱ͕Ͱ͖ΔͷɺίϧʔνϯλεΫʢͳͲʣ -
コルーチンを実⾏ >>> print(customers_long_thinking_order()) <coroutine object customers_long_thinking_order at 0x10d949ec0> def customers_long_thinking_order():
await asyncio.sleep(10000) return “͟Δͦ” async -
コルーチンを実⾏ >>> print(customers_long_thinking_order()) <coroutine object customers_long_thinking_order at 0x10d949ec0> ݁ՌΛड͚औΕͳ͍ def
customers_long_thinking_order(): await asyncio.sleep(10000) return “͟Δͦ” async -
イベントループを作る async def order(): print(“͝จʁ”) menu = await long_thinking() print(menu)
async def long_thinking(): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ” -
イベントループを作る async def order(): print(“͝จʁ”) menu = await long_thinking() print(menu)
async def long_thinking(): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ” asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ -
イベントループを作る async def order(): print(“͝จʁ”) menu = await long_thinking() print(menu)
async def long_thinking(): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ” asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ ͝จʁ ͪΐͬͱͬͯͶ -
イベントループを作る async def order(): print(“͝จʁ”) menu = await long_thinking() print(menu)
async def long_thinking(): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ” asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ ͝จʁ ͪΐͬͱͬͯͶ # …ߟͷͷͪ 😴 -
イベントループを作る async def order(): print(“͝จʁ”) menu = await long_thinking() print(menu)
async def long_thinking(): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ” asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ ͝จʁ ͪΐͬͱͬͯͶ ͟Δͦ # ίϧʔνϯͷΓΛ ड͚औͬͯprint # …ߟͷͷͪ 😴 -
イベントループを作る async def order(): print(“͝จʁ”) menu = await long_thinking() print(menu)
async def long_thinking(): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ” asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ ͝จʁ ͪΐͬͱͬͯͶ ͟Δͦ # ίϧʔνϯͷΓΛ ड͚औͬͯprint # …ߟͷͷͪ 😴 -
イベントループを作る async def order(): print(“͝จʁ”) menu = await long_thinking() print(menu)
async def long_thinking(): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ” asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ -
イベントループを作る async def order(): print(“͝จʁ”) menu = await long_thinking() print(menu)
async def long_thinking(): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ” asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ -
イベントループを作る async def order(): print(“͝จʁ”) menu = await long_thinking() print(menu)
async def long_thinking(): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ” asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ -
イベントループを作る def order(): print(“͝จʁ”) menu = long_thinking() print(menu) def long_thinking():
print(“ͪΐͬͱͬͯͶ”) sleep(10000) return “͟Δͦ” order() # ࣮ߦ͢Δ -
イベントループを作る async def order(): print(“͝จʁ”) menu = await long_thinking() print(menu)
async def long_thinking(): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ” asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ -
async def long_thinking(): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep() return “͟Δͦ” await の動作を確認する
async def order(): print(“͝จʁ”) menu = await long_thinking() print([menu]) -
async def long_thinking(num: int): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ”
await の動作を確認する async def order(): print(“͝จʁ”) menu = await long_thinking() print([menu]) -
await の動作を確認する async def order(): print(“͝จʁ”) menu = await long_thinking()
print([menu]) async def long_thinking(num: int): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ” -
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 の動作を確認する -
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()) -
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 -
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()) -
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) -
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) -
初めてのタスク 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()) -
初めてのタスク 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, “͟Δͦ” -
初めてのタスク 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()) -
初めてのタスク 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, “͟Δͦ” -
初めてのタスク 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 -
初めてのタスク 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()) 実⾏してみましょう -
初めてのタスク 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, “͟Δͦ” -
初めてのタスク 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, “͟Δͦ” -
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()) -
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 -
ίϧʔνϯ asyncioはこわくない λεΫ ίϧʔνϯΛΠϕϯτϧʔϓʹొɻ ྑ͖ʹܭΒͬͯΓସ͑ͯ͘ΕΔɻฒߦॲཧͯ͘͠ΕΔɻ ؔʹ async Λ͚ͭΔ͚ͩ ؊await async
def await λεΫ ·ͨ ίϧʔνϯ create_task(ίϧʔνϯ) - Πϕϯτϧʔϓʹొ gather(λεΫ ·ͨ ίϧʔνϯ) - ·ͱΊͯ݁ՌΛड͚औΕΔ -
シンプルな asyncio のサンプル async def long_thinking(): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(10000) return
“͟Δͦ” asyncio.sleep(10000) -
考え中のお客様が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) -
考え中のお客様が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) -
考え中のお客様が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) -
考え中のお客様が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 -
考え中のお客様が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) -
考え中のお客様が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 -
考え中のお客様が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) -
考え中のお客様が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 -
考え中のお客様が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) -
考え中のお客様が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 -
ίϧʔνϯ asyncioはこわくない λεΫ Πϕϯτϧʔϓ create_task(ίϧʔνϯ) - Πϕϯτϧʔϓʹొ gather(λεΫ ·ͨ ίϧʔνϯ)
- ·ͱΊͯ݁ՌΛड͚औΕΔ async def await λεΫ ·ͨ ίϧʔνϯ asynio.run(ίϧʔνϯ) -
ίϧʔνϯ asyncioはこわくない λεΫ Πϕϯτϧʔϓ create_task(ίϧʔνϯ) - Πϕϯτϧʔϓʹొ gather(λεΫ ·ͨ ίϧʔνϯ)
- ·ͱΊͯ݁ՌΛड͚औΕΔ async def await λεΫ ·ͨ ίϧʔνϯ asynio.run(ίϧʔνϯ) まずはこれを抑えましょう -
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 -
asyncioはこわくない 緩やかな根拠 Python 3.7 ͷΞοϓσʔτ asyncio.create_task(), asyncio.gather(), asyncio.run() ͷՃ Yury
Selivanov ʢϢʔϦʔηϦόϊϑʣࢯ PEP 492 -- Coroutines with async and await syntax ஶऀ -
asyncioはこわくない 緩やかな根拠 Python 3.7 ͷΞοϓσʔτ asyncio.create_task(), asyncio.gather(), asyncio.run() ͷՃ Yury
Selivanov ʢϢʔϦʔηϦόϊϑʣࢯ PEP 492 -- Coroutines with async and await syntax ஶऀ normal ϋʔυίΞ -
asyncioはこわくない 緩やかな根拠 Python 3.7 ͷΞοϓσʔτ asyncio.create_task(), asyncio.gather(), asyncio.run() ͷՃ Yury
Selivanov ʢϢʔϦʔηϦόϊϑʣࢯ PEP 492 -- Coroutines with async and await syntax ஶऀ -
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 -
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ΦϒδΣΫτͷࢦఆ͕ෆՄ -
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ར༻Մೳʹ -
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ར༻Մೳʹ Ξοϓσʔτ͕͍ -
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ར༻Մೳʹ Ξοϓσʔτ͕͍ ༗༻ͳαϯϓϧίʔυաڈͷόʔδϣϯΛϕʔεʹ͍ͯ͠Δͷଟ͍ -
ίϧʔνϯ asyncioはこわくない λεΫ Πϕϯτϧʔϓ create_task(ίϧʔνϯ) - Πϕϯτϧʔϓʹొ gather(λεΫ ·ͨ ίϧʔνϯ)
- ·ͱΊͯ݁ՌΛड͚औΕΔ async def await λεΫ ·ͨ ίϧʔνϯ asynio.run(ίϧʔνϯ) まずはこれを抑えましょう -
コ asyncio ちょっとわがまま 外部IOの発⽣する処理 HTTPアクセス(APIとか) データベースアクセス urllib.requests requests psycopg2 mysqlclient
aiohttpやhttpx asyncpg aiomysql -
コ asyncio ちょっとわがまま 外部IOの発⽣する処理 HTTPアクセス(APIとか) データベースアクセス urllib.requests requests psycopg2 mysqlclient
aiohttpやhttpx asyncpg "XFTPNF IUUQTHJUIVCDPNUJNPGVSSFSBXFTPNFBTZODJP BJPMJCT IUUQTHJUIVCDPNBJPMJCT FODPEF IUUQTXXXFODPEFJPQSPKFDUT aiomysql -
コ asyncio ちょっとわがまま 外部IOの発⽣する処理 HTTPアクセス(APIとか) データベースアクセス urllib.requests requests psycopg2 mysqlclient
aiohttpやhttpx asyncpg aiomysql "XFTPNF IUUQTHJUIVCDPNUJNPGVSSFSBXFTPNFBTZODJP BJPMJCT IUUQTHJUIVCDPNBJPMJCT FODPEF IUUQTXXXFODPEFJPQSPKFDUT -
•ASGI = asyncioରԠ WebϑϨʔϜϫʔΫͰasyncio͕͑Δ ASGIΞϓϦέʔγϣϯ •asyncioͷಘҙͳ͜ͱ = ඇಉظIO ֎෦ґଘͷσʔλऔಘ •࣮ͯ͞ࡍͷίʔυΛݟͯΈ·͠ΐ͏
•ʢFastAPIˍasyncpgΛྫʹʣ asyncpg -
•PostgreSQLͷσʔλϕʔεΠϯλʔϑΣʔεϥΠϒϥϦ asyncpg •asyncioରԠ͍ͯ͠Δ •MagicStack ࣾͷϦϙδτϦ uvloopಉ͡ϦϙδτϦ •ઌ΄Ͳհͨ͠ async/await ͷPEPஶऀ Yury
Selivanov ʢϢʔϦʔηϦόϊϑʣࢯ •ORMͰར༻ՄೳʢSQLAlchemy databases ʣ って -
•PostgreSQLͷσʔλϕʔεΠϯλʔϑΣʔεϥΠϒϥϦ asyncpg •asyncioରԠ͍ͯ͠Δ •MagicStack ࣾͷϦϙδτϦ uvloopಉ͡ϦϙδτϦ •ઌ΄Ͳհͨ͠ async/await ͷPEPஶऀ Yury
Selivanov ʢϢʔϦʔηϦόϊϑʣࢯ •ORMͰར༻ՄೳʢSQLAlchemy databases ʣ って -
asyncpg の簡単な使いかた # ίωΫγϣϯϓʔϧΛऔಘ async def main(): async with asyncpg.create_pool(dns)
as pool: await pool.execute(...) dns = "postgresql://user:pass@host:5432/database" import asyncpg -
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/ -
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" -
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) -
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 -
asyncpg と FastAPI app = FastAPI() db = Database() from
fastapi import FastAPI, Request -
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 -
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 -
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) -
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 -
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 -
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 -
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 -
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 -
•ίϧʔνϯා͘ͳ͍ - 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. 🙏