Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
ASGIについて
Search
Kaito
May 07, 2021
Programming
0
280
ASGIについて
コード例
Kaito
May 07, 2021
Tweet
Share
More Decks by Kaito
See All by Kaito
CGと機械学習
kaitolucifer
0
170
Other Decks in Programming
See All in Programming
Team operations that are not burdened by SRE
kazatohiei
1
260
都市をデータで見るってこういうこと PLATEAU属性情報入門
nokonoko1203
1
570
Modern Angular with Signals and Signal Store:New Rules for Your Architecture @enterJS Advanced Angular Day 2025
manfredsteyer
PRO
0
130
PostgreSQLのRow Level SecurityをPHPのORMで扱う Eloquent vs Doctrine #phpcon #track2
77web
2
390
AIコーディング道場勉強会#2 君(エンジニア)たちはどう生きるか
misakiotb
1
250
Select API from Kotlin Coroutine
jmatsu
1
190
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
47
31k
Bytecode Manipulation 으로 생산성 높이기
bigstark
2
380
LT 2025-06-30: プロダクトエンジニアの役割
yamamotok
0
540
Blazing Fast UI Development with Compose Hot Reload (droidcon New York 2025)
zsmb
1
240
Deep Dive into ~/.claude/projects
hiragram
9
1.6k
iOSアプリ開発で 関数型プログラミングを実現する The Composable Architectureの紹介
yimajo
2
220
Featured
See All Featured
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
The Language of Interfaces
destraynor
158
25k
Gamification - CAS2011
davidbonilla
81
5.3k
A better future with KSS
kneath
239
17k
Java REST API Framework Comparison - PWX 2021
mraible
31
8.6k
Done Done
chrislema
184
16k
Code Review Best Practice
trishagee
68
18k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
181
53k
We Have a Design System, Now What?
morganepeng
53
7.7k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
Mobile First: as difficult as doing things right
swwweet
223
9.7k
Transcript
ASGIについて
⽬次 1. WSGIとは 2. WSGIの限界 3. コルーチン 4. Pythonのコルーチン 5.
イベントループ 6. Pythonのイベントループ 7. ASGIとは 8. ASGIの問題点 2
WSGIとは n PEP333で提案され、PEP3333でアップグレードされたPythonのWebサーバーインターフェース Ø Pythonにおいて、WebアプリケーションとWebサーバーのインタラクションをスタンダート 化する n WSGIフレームワーク n WSGIサーバー
3 ASGIについて mod_wsgi
WSGIとは n WSGIサーバーとWSGIアプリケーション 4 ASGIについて 画像参照:https://www.cabotsolutions.com/a-detailed-study-of-wsgi-web-server-gateway-interface-of-python
WSGIとは n WSGIアプリケーションのインターフェース 5 ASGIについて
WSGIの限界 n 公式ではwebsocketに対応していない Ø は公式の解決策ではない 6 ASGIについて 画像参照: https://ja.javascript.info/websocket
WSGIの限界 n HTTP/2に対応してない Ø WSGIサーバーがWSGIアプリケーションを呼び出した後は戻り値が戻ってくるまでブロッキ ングする(基本的には) 7 ASGIについて 画像参照:https://medium.com/475cumulus/wsgi-is-not-enough-anymore-part-i-bc9713a79841
WSGIの限界 n ネイティブコルーチンと連携できない Ø asyncとawaitが使えない Ø gevent workerなどを使⽤する場合、既存のイベントループと別に、新しいイベントループを 作る必要がある Ø
Python3.7から使えるasyncio.runも実は新しいイベントループを作っている 8 ASGIについて
コルーチン 9 ASGIについて n ⼀時中断や再開が可能なプログラミング構造
Pythonのコルーチン n ネイティブコルーチン Ø async、awaitで実現されたコルーチン n ジェネレーターベースのコルーチン Ø ジェネレーターのyield,yield from,sendなどで実現されたコルーチン
Ø 現在は⾮推奨 n greenlet Ø サードパーティのC⾔語拡張で実現されたコルーチン Ø geventなどで、標準ライブラリのio処理を⾮同期化できる(モンキーパッチ) Ø サードパーティと⾊々互換性の問題がある 10 ASGIについて
イベントループ 11 ASGIについて 画像参照: https://dev.to/djangostars/asynchronous-programming-in-python--asyncio-guide-2c61
Pythonイベントループ n asyncioのデフォルトイベントループ Ø 基本的に遅い n uvloop Ø nodejsで使われたlibuvのPythonバインディング n
curio Ø asyncioの代替で、独⾃のイベントループを実装 n trio Ø asyncioの代替で、独⾃のイベントループを実装 12 ASGIについて
ASGIとは n Django Software Foundationが提案したPythonの⾮同期サーバーインターフェース Ø https://channels.readthedocs.io/en/stable/asgi.html Ø WSGIの各制限を解決するためのもの n
ASGIフレームワーク n ASGIサーバー 13 ASGIについて Daphne
ASGIとは n ASGIアプリケーションのインターフェース 14 ASGIについて
ASGIとは n ⾼速 Ø 同期WSGIサーバー・アプリケーションより2倍以上速い n リソースの利⽤率が⾼い Ø CPUのアイドル時間を減らせる n
WebSocketとHTTP/2の対応 15 ASGIについて 画像参照: https://florimond.dev/blog/articles/2019/08/introduction-to-asgi-async-python-web/
ASGIの問題点 n 同期ioと混在すると、⾜が引っ張られる Ø all inする必要がある n ライブラリはまだ充実してない Ø 例えば、⾮同期ORMはTortoise
ORMぐらいしかない n エラーハンドルが難しい Ø trioはこの問題に特化したライブラリ 16 ASGIについて