Share
Embed
Django 3.2 ASGI対応 - こわくない asyncio 基礎とasync viewの使い所
DjangoCongressJP 2021 の登壇資料です。
Junya Fukuda
July 03, 2021
Transcript
238 slides
-
Django 3.2 ASGI対応 Junya Fukuda DjangoCongress JP 2021 -こわくない asyncio
基礎とasync viewの使い所 -
•ా ൏ʢJunya Fukudaʣʢ@JunyaFffʣ •גࣜձࣾຊγεςϜٕݚʢJSLʣॴଐ ݝͷձࣾ •WebΤϯδχΞ •GEEKLAB.NAGANO - ίϛϡχςΟεϖʔεӡӦ •Effective
Python ͷಡॻձͬͯ·͢ 📚 •ʢ͓͠͝ͱͰʣόϦϡʔϒοΫε •ຊ͖ʹѪ͞ΕΔαʔϏεΛࢦͯ͠ʢݹຊങऔɾൢചʣ ΪʔΫϥϘಡॻձ -
•Django 3 ͕ग़͔ͯΒͷॳΊͯͷDjangoCongress JP Django 3.2 •Django 3 ͷۄͷ1ͭ •Django
3.0 - ASGIରԠ •Django 3.1 - Async View, ϛυϧΣΞ, ςετΫϥΠΞϯτ ͷαϙʔτ -
•Django 3 ͕ग़͔ͯΒͷॳΊͯͷDjangoCongress Django 3.2 •Django 3 ͷۄͷ1ͭ •Django 3.0
- ASGIରԠ •Django 3.1 - Async View, ϛυϧΣΞ, ςετΫϥΠΞϯτ ͷαϙʔτ -
•Django 3 ͕ग़͔ͯΒͷॳΊͯͷDjangoCongress Django 3.2 •Django 3 ͷۄͷ1ͭ •Django 3.0
- ASGIରԠ •Django 3.1 - Async View, ϛυϧΣΞ, ςετΫϥΠΞϯτ ͷαϙʔτ •Django 3.x or 4? - Async ORM -
Django 3.0 - ASGI対応 •WebαʔόͱϑϨʔϜϫʔΫΛͭͳ͙ɺ༷ •WSGIͷਫ਼ਆతޙܧ •Gunicorn uWSGI ⁶
Django Flask, Pyramid って? -
Django 3.0 - ASGI対応 •WebαʔόͱϑϨʔϜϫʔΫΛͭͳ͙ɺ༷ •WSGIͷਫ਼ਆతޙܧ •Gunicorn uWSGI ⁶
Django Flask, Pyramid •Uvicorn Hypercorn ⁶ Django FastAPI, Starlette って? -
Django 3.0 - ASGI対応 •WebαʔόͱϑϨʔϜϫʔΫΛͭͳ͙ɺ༷ •WSGIͷਫ਼ਆతޙܧ •Gunicorn uWSGI ⁶
Django Flask, Pyramid •Uvicorn Hypercorn ⁶ Django FastAPI, Starlette •WSGIͱASGIɺͳʹ͕ҧ͏ͷ͔ って? WSGI -
•Django 3 ͕ग़͔ͯΒͷॳΊͯͷDjangoCongress Django 3.2 •Django 3 ͷۄͷ1ͭ •Django 3.0
- ASGIରԠ •Django 3.1 - Async View, ϛυϧΣΞ, ςετΫϥΠΞϯτ ͷαϙʔτ •Django 3.x or 4? - Async ORM -
•Django 3 ͕ग़͔ͯΒͷॳΊͯͷDjangoCongress Django 3.2 •Django 3 ͷۄͷ1ͭ •Django 3.0
- ASGIରԠ •Django 3.1 - Async View, ϛυϧΣΞ, ςετΫϥΠΞϯτ ͷαϙʔτ •Django 3.x or 4? - Async ORM asyncioରԠ -
本⽇のおしながき •Django 3.1 - Async View ͷ͍ॴ •Django 3.x or
4? - Async ORM •ASGI Λར༻͢Δ্Ͱඞཁͳ asyncio جૅ -
•asyncio͜Θ͍ͳ͋… •Django async view ͬͯͳʹ͕͓͍͍͠ͷʁ •Django ORM ͍͚Δͷʁʁ •asyncio ͪΐͬͱා͘ͳ͘ͳ͔ͬͨ
👋 •Django async view ͬͯͦ͏͍͏ͱ͖Ͷ 🙌 -
•asyncio͜Θ͍ͳ͋… •Django async view ͬͯͳʹ͕͓͍͍͠ͷʁ •Django ORM ͍͚Δͷʁʁ •asyncio ͪΐͬͱා͘ͳ͘ͳ͔ͬͨ
👋 •Django async view ͬͯͦ͏͍͏ͱ͖Ͷ 🙌 •Django async ORM ͨͷ͠Έͩͳ͋ʙ ✊ -
•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 -
•͜Θ͔ͬͨ •ίϧʔνϯා͘ͳ͍ - Minimum Viable Programmer ʢϒϩάʣ •Using Asyncio in
Python - ΦϥΠϦʔ ʢ🐸ຊʣ asyncio めっちゃこわい •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 シングルスレッド = ワンオペ どのタイミングでタスクの切り替えをするのでしょうか 🤔 -
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 Λ͏্Ͱͷجຊͷ͖ •ॲཧΛ్தͰதஅͯ͠࠶։Ͱ͖Δϧʔνϯ -
def customers_long_thinking_order(): await asyncio.sleep(10000) return “͟Δͦ” 時間のかかる処理にawait async •࣌ؒͷ͔͔ΔॲཧʹawaitΛ͚ͭΔ •͔ͦ͜Βதஅͱ࠶։͢Δ
•await Λ͚ͭΔ͜ͱ͕Ͱ͖ΔͷɺίϧʔνϯλεΫʢͳͲʣ -
def customers_long_thinking_order(): await asyncio.sleep(10000) return “͟Δͦ” コルーチンを実⾏ >>> print(customers_long_thinking_order()) async
<coroutine object customers_long_thinking_order at 0x10d949ec0> -
•Python 3.7 ͰՃ͞Εͨ asyncio.run() イベントループ •ΠϕϯτϧʔϓΛ࡞Δ •λεΫ͕ऴΘͬͨΒআͯ͘͠ΕΔ •asyncio.run(corutine) Λड͚औΔ͜ͱ͕Ͱ͖Δ •loopΦϒδΣΫτ
-> asyncio.get_running_loop() / run_until_complete() -
async def customers_long_thinking_order(): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(10000) return “͟Δͦ” イベントループを作る async
def order(): print(“͝จʁ”) menu = await customers_long_thinking_order() print(menu) -
イベントループを作る ͝จʁ ͪΐͬͱͬͯͶ # …ߟͷͷͪ ͟Δͦ asyncio.run(order()) # ΠϕϯτϧʔϓΛ࡞Δ #
ίϧʔνϯͷΓΛड͚औͬͯprint -
async def customers_long_thinking_order(): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep() return “͟Δͦ” await の動作を確認する
async def order(): print(“͝จʁ”) menu1 = await customers_long_thinking_order() menu2 = await customers_long_thinking_order() menu2 = await customers_long_thinking_order() print([menu1, memu2, menu3]) -
async def customers_long_thinking_order(num: int): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(num) return “͟Δͦ” async
def order(): print(“͝จʁ”) menu1 = await customers_long_thinking_order() menu2 = await customers_long_thinking_order() menu2 = await customers_long_thinking_order() print([menu1, memu2, menu3]) await の動作を確認する -
async def customers_long_thinking_order(num: int): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ”
async def order(): print(“͝จʁ”) menu1 = await customers_long_thinking_order(1) menu2 = await customers_long_thinking_order(2) menu3 = await customers_long_thinking_order(3) print([menu1, memu2, menu3]) await の動作を確認する 並⾏実⾏すると3秒で終わるはず asyncio.run(order()) -
async def customers_long_thinking_order(num: int): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ”
async def order(): print(“͝จʁ”) menu1 = await customers_long_thinking_order(1) menu2 = await customers_long_thinking_order(2) menu3 = await customers_long_thinking_order(3) print([menu1, memu2, menu3]) await の動作を確認する 並⾏実⾏すると3秒で終わるはず asyncio.run(order()) l͝จʁz ͪΐͬͱͬͯͶ ͪΐͬͱͬͯͶ ͪΐͬͱͬͯͶ < ͟Δͦ ͟Δͦ ͟Δͦ > UJNF -
async def customers_long_thinking_order(num: int): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ”
async def order(): print(“͝จʁ”) menu1 = await customers_long_thinking_order(1) print(menu1) menu2 = await customers_long_thinking_order(2) print(menu2) menu3 = await customers_long_thinking_order(3) print(menu3) await の動作を確認する 並⾏実⾏すると3秒で終わるはず asyncio.run(order()) -
async def customers_long_thinking_order(num: int): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ”
async def order(): print(“͝จʁ”) menu1 = await customers_long_thinking_order(1) print(menu1) menu2 = await customers_long_thinking_order(2) print(menu2) menu3 = await customers_long_thinking_order(3) print(menu3) await の動作を確認する 並⾏実⾏すると3秒で終わるはず asyncio.run(order()) l͝จʁz ͪΐͬͱͬͯͶ ͟Δͦ ͪΐͬͱͬͯͶ ͟Δͦ ͪΐͬͱͬͯͶ b͟Δͦ UJNF -
async def customers_long_thinking_order(num: int): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(num) return num, “͟Δͦ”
async def order(): print(“͝จʁ”) menu1 = await customers_long_thinking_order(1) print(menu1) menu2 = await customers_long_thinking_order(2) print(menu2) menu3 = await customers_long_thinking_order(3) print(menu3) await の動作を確認する 並⾏実⾏すると3秒で終わるはず asyncio.run(order()) コルーチンをawaitするだけでは 並⾏にならない 1つのコルーチンが終わってから 次のコルーチン 普通の関数実⾏ と同じ l͝จʁz ͪΐͬͱͬͯͶ ͟Δͦ ͪΐͬͱͬͯͶ ͟Δͦ ͪΐͬͱͬͯͶ b͟Δͦ UJNF -
初めてのタスク async def customers_long_thinking_order(num: int): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(num) return num,
“͟Δͦ” async def order(): print(“͝จʁ”) menu1 = await customers_long_thinking_order(1) menu2 = await customers_long_thinking_order(2) menu3 = await customers_long_thinking_order(3) print([menu1, memu2, menu3]) asyncio.run(order()) -
初めてのタスク async def order(): print(“͝จʁ”) menu1 = await customers_long_thinking_order(1) menu2
= await customers_long_thinking_order(2) menu3 = await customers_long_thinking_order(3) print([menu1, memu2, menu3]) asyncio.run(order()) async def customers_long_thinking_order(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(customers_long_thinking_order(1)) task2 = asyncio.create_task(customers_long_thinking_order(2)) task3 = asyncio.create_task(customers_long_thinking_order(3)) menu1 = await task1 menu1 = await task2 menu1 = await task3 print([menu1, memu2, menu3]) asyncio.run(order()) -
初めてのタスク async def customers_long_thinking_order(num: int): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(num) return num,
“͟Δͦ” async def order(): print(“͝จʁ”) task1 = asyncio.create_task(customers_long_thinking_order(1)) task2 = asyncio.create_task(customers_long_thinking_order(2)) task3 = asyncio.create_task(customers_long_thinking_order(3)) menu1 = await task1 menu1 = await task2 menu1 = await task3 print([menu1, memu2, menu3]) asyncio.run(order()) 実⾏してみましょう -
初めてのタスク async def customers_long_thinking_order(num: int): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(num) return num,
“͟Δͦ” async def order(): print(“͝จʁ”) task1 = asyncio.create_task(customers_long_thinking_order(1)) task2 = asyncio.create_task(customers_long_thinking_order(2)) task3 = asyncio.create_task(customers_long_thinking_order(3)) menu1 = await task1 menu2 = await task2 menu3 = await task3 print([menu1, memu2, menu3]) asyncio.run(order()) 実⾏してみましょう ͪΐͬͱͬͯͶ ͪΐͬͱͬͯͶ ͪΐͬͱͬͯͶ < ͟Δͦ ͟Δͦ ͟Δͦ > UJNF -
初めてのタスク async def customers_long_thinking_order(num: int): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(num) return num,
“͟Δͦ” async def order(): print(“͝จʁ”) task1 = asyncio.create_task(customers_long_thinking_order(1)) task2 = asyncio.create_task(customers_long_thinking_order(2)) task3 = asyncio.create_task(customers_long_thinking_order(3)) menu1 = await task1 print(menu1) menu2 = await task2 print(menu2) menu3 = await task3 print(menu2) asyncio.run(order()) 実⾏してみましょう -
初めてのタスク async def customers_long_thinking_order(num: int): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(num) return num,
“͟Δͦ” async def order(): print(“͝จʁ”) task1 = asyncio.create_task(customers_long_thinking_order(1)) task2 = asyncio.create_task(customers_long_thinking_order(2)) task3 = asyncio.create_task(customers_long_thinking_order(3)) menu1 = await task1 print(menu1) menu2 = await task2 print(menu2) menu3 = await task3 print(menu2) asyncio.run(order()) 実⾏してみましょう l͝จʁz ͪΐͬͱͬͯͶ ͪΐͬͱͬͯͶ ͪΐͬͱͬͯͶ ͟Δͦ ͟Δͦ ͟Δͦ UJNF -
初めてのタスク async def customers_long_thinking_order(num: int): print(“ͪΐͬͱͬͯͶ”) await asyncio.sleep(num) return num,
“͟Δͦ” async def order(): print(“͝จʁ”) task1 = asyncio.create_task(customers_long_thinking_order(1)) task2 = asyncio.create_task(customers_long_thinking_order(2)) task3 = asyncio.create_task(customers_long_thinking_order(3)) menu1 = await task1 print(menu1) menu2 = await task2 print(menu2) menu3 = await task3 print(menu2) asyncio.run(order()) 実⾏してみましょう l͝จʁz ͪΐͬͱͬͯͶ ͪΐͬͱͬͯͶ ͪΐͬͱͬͯͶ ͟Δͦ ͟Δͦ ͟Δͦ UJNF -
gatherを利⽤して結果をまとめて受け取る async def customers_long_thinking_order(num: int): print("ͪΐͬͱͬͯͶ") await asyncio.sleep(num) return num,
"͟Δͦ" async def order(): print("“͝จʁ”") tasks = [ customers_long_thinking_order(3000), customers_long_thinking_order(200), customers_long_thinking_order(10), ] result = await asyncio.gather(*tasks) print(result) asyncio.run(order()) -
gatherを利⽤して結果をまとめて受け取る async def customers_long_thinking_order(num: int): print("ͪΐͬͱͬͯͶ") await asyncio.sleep(num) return num,
"͟Δͦ" async def order(): print("“͝จʁ”") tasks = [ customers_long_thinking_order(3000), customers_long_thinking_order(200), customers_long_thinking_order(10), ] result = await asyncio.gather(*tasks) print(result) asyncio.run(order()) l͝จʁz ͪΐͬͱͬͯͶ ͪΐͬͱͬͯͶ ͪΐͬͱͬͯͶ < ͟Δͦ ͟Δͦ ͟Δͦ > 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()) 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=͓͍͍͓͠ڶഴʁ") 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 ͪΐͬͱͬͯͶ ͪΐͬͱͬͯͶ ͪΐͬͱͬͯͶ < 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) 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(coro) - Πϕϯτϧʔϓʹొ gather(λεΫ ·ͨ ίϧʔνϯ)
- ·ͱΊͯ݁ՌΛड͚औΕΔ async def hoge() await λεΫ ·ͨ ίϧʔνϯ asynio.run(ίϧʔνϯ) ༨ྗ͕͋Εɺget_running_loop()ͬͯͷ 🙏 -
ίϧʔνϯ asyncioはこわくない λεΫ Πϕϯτϧʔϓ create_task(coro) - Πϕϯτϧʔϓʹొ gather(λεΫ ·ͨ ίϧʔνϯ)
- ·ͱΊͯ݁ՌΛड͚औΕΔ async def hoge() await λεΫ ·ͨ ίϧʔνϯ asynio.run(ίϧʔνϯ) ༨ྗ͕͋Εɺget_running_loop()ͬͯͷ 🙏 まずはこれを抑えましょう -
本⽇のおしながき •Django 3.1 - Async View •Django 3.x or 4?
- Async ORM •ASGI Λར༻͢Δ্Ͱඞཁͳ asyncio جૅ -
Django 3.2 ASGI対応 Junya Fukuda DjangoCongress JP 2021 -こわくない asyncio
基礎とasync viewの使い所 -
Django 3.2 ASGI対応 Junya Fukuda DjangoCongress JP 2021 -こわくない asyncio
基礎とasync viewの使い所 -
Django Async View ͷ͍ॴ •IOͷൃੜ͢ΔॲཧͰޮՌΛൃش͢Δ •ྫ͑APIͷϦΫΤετ •ྫ͑IoTσόΠεͱͷMQTT •ASGIରԠͱasync view •asyncio
ඇಉظIO ͷྑ͖ͱ͜ΖΛڗडͰ͖Δ •DjangoΛར༻͍ͨ͠߹ -
Django Async View の使い所 •ιʔεͷྫ async def long_think(client, num: int):
print("ͪΐͬͱͬͯͶ") response = await client.get("https://www.google.com/search?q=͓͍͍͓͠ڶഴʁ") return num, response, "͟Δͦ" async def async_view(request): print("“͝จʁ”") async with httpx.AsyncClient() as client: tasks = [long_think(client) for i in range(10)] result = await asyncio.gather(*tasks) return HttpResponse(result) -
Django Async View の使い所 •ιʔεͷྫ async def long_think(client, num: int):
print("ͪΐͬͱͬͯͶ") response = await client.get("https://www.google.com/search?q=͓͍͍͓͠ڶഴʁ") return num, response, "͟Δͦ" async def async_view(request): print("“͝จʁ”") async with httpx.AsyncClient() as client: tasks = [long_think(client) for i in range(10)] result = await asyncio.gather(*tasks) return HttpResponse(result) BTZODJPSVO ͠ͳ͍ͷʁ🤔 -
Django Async View の使い所 •Έͳ͞Μ͏ͯ͠·͢ɻ •ΒͣΒͣͷ͏ͪʹɻ •ASGI Django ͬͯͲ͏࣮ͬͯߦ͠·͔͢ʁ $
uvicorn hello_async_world.asgi:application -
Django Async View の使い所 •Έͳ͞Μ͏ͯ͠·͢ɻ •ΒͣΒͣͷ͏ͪʹɻ •ASGI Django ͬͯͲ͏࣮ͬͯߦ͠·͔͢ʁ $
uvicorn hello_async_world.asgi:application •ASGIͰɺPythonͷWebαʔόͱPythonͷWeb FWΛܨ͍ͰΔ -
Django Async View の使い所 •Έͳ͞Μ͏ͯ͠·͢ɻ •ΒͣΒͣͷ͏ͪʹɻ •ASGI Django ͬͯͲ͏࣮ͬͯߦ͠·͔͢ʁ $
uvicorn hello_async_world.asgi:application •ASGIͰɺPythonͷWebαʔόͱPythonͷWeb FWΛܨ͍ͰΔ •ASGIαʔόଆͰΠϕϯτϧʔϓΛ࣮ߦ͍ͯ͠Δ -
Django Async View の使い所 •sync_to_async() ಉظؔΛड͚औΓɺͦΕΛϥοϓͯ͠ඇಉظؔΛฦ͠·͢ɻ ϥούʔ·ͨσίϨʔλͱͯ͠༻Ͱ͖·͢ɻ async_function = sync_to_async(sync_function,
thread_sensitive=False) async_function = sync_to_async(sensitive_sync_function, thread_sensitive=True) @sync_to_async def sync_function(...): ... IUUQTEPDTEKBOHPQSPKFDUDPNFOUPQJDTBTZODTZODUPBTZOD -
Django Async View の使い所 •sync_to_async() asyncioରԠ͍ͯ͠ͳ͍ಉظॲཧΛΠϕϯτϧʔϓʹొ͢Δ͜ͱ͕Ͱ͖·͢ɻ ʢDjangoʹݶͬͨͰͳ͘ʣ concurrent.futures Ϟδϡʔϧ ʢthreading
ͱ multiprocessing Λ༰қʹ͑Δඪ४ϥΠϒϥϦʣ -
Django Async View の使い所 •sync_to_async() asyncioରԠ͍ͯ͠ͳ͍ಉظॲཧΛΠϕϯτϧʔϓʹొ͢Δ͜ͱ͕Ͱ͖·͢ɻ ʢDjangoʹݶͬͨͰͳ͘ʣ concurrent.futures Ϟδϡʔϧ ʢthreading
ͱ multiprocessing Λ༰қʹ͑Δඪ४ϥΠϒϥϦʣ ͜ΕͰඇಉظʹͰ͖Δ…ʹͰ͖·͕͢ɺ ࣮ߦʹίετ͕͔͔ΓʹӨڹ͕ଟগ͋Γ·͢ɻ -
Django Async View の使い所 •ASGIରԠͱasync view •͜ΕʹΑͬͯɺDjangoͰasyncioʹΑΔඇಉظIO/ฒߦॲཧͷԸܙ •ಉظॲཧ concurrent.futures Λͬͨϥοϓ͕Մೳ
•·͍ͩͬͯΔಉظॲཧ •ͦ͏ɻ࠷ޙͷࡆɻDjangoͷORM͕͍ͬͯΔɻʢasyncio ະରԠʣ -
Async ORM •Async ORM ͷಓ •PyConline AU 2020 "Taking Django's
ORM Async” •Django ίΞ developer Andrew Godwin ͞Μ -
Async ORM •Async ORM ͷಓ •APIͷσβΠϯ͕ॏཁͩ •ͨͩɺasync DBAPI ·ͩͳ͍… •՝͋Δ
•Model API / ΫΤϦ / DBΞμϓλʔ ͱରԠ͍ͯ͘͠ܭը -
まとめ •asyncio ଟগ͜Θ͘ͳ͘ͳͬͨͰ͠ΐ͏͔ コ •Django async view - ಉظͱඇಉظ߹Θͤͯ IOόϯυͳॲཧΛத৺ʹ…
•Async ORM - ָ͠ΈͰ͢Ͷʂ ʢPyhtonͰ asyncio ରԠͷORMΞμϓλʔ͢Ͱʹ͍͔ͭ͋͘Γ·͢ɻʣ ·ͨɺDjangoOSSͰ͢ɻϦϙδτϦΛͷ͍ͧͯΈΔͷྑ͍͔͠Ε·ͤΜ 👀 IUUQTHJUIVCDPNEKBOHPEKBOHP -
•ίϧʔνϯා͘ͳ͍ - Minimum Viable Programmer •Using Asyncio in Python -
Oreilly & Associates Inc 参考 •Better Examples of Django Async Views •https://dev.to/arocks/better-examples-of-django-async-views-295d •asyncioͷTaskʹؔ͢Δجૅࣝ •https://aish.dev/python/20200711_asyncio_task.html •DjangoCon 2020 | How To Break Django: With Async - Andrew Godwin •https://www.youtube.com/watch?v=19Uh_PA_8Rc •"Taking Django's ORM Async" - Andrew Godwin (PyConline AU 2020) •https://youtu.be/ibAmA4QQDhs