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
「Cursor/Devin全社導入の理想と現実」のその後
saitoryc
0
830
Result型で“失敗”を型にするPHPコードの書き方
kajitack
5
920
たった 1 枚の PHP ファイルで実装する MCP サーバ / MCP Server with Vanilla PHP
okashoi
1
260
なんとなくわかった気になるブロックテーマ入門/contents.nagoya 2025 6.28
chiilog
1
270
ruby.wasmで多人数リアルタイム通信ゲームを作ろう
lnit
3
490
Team operations that are not burdened by SRE
kazatohiei
1
310
Hack Claude Code with Claude Code
choplin
4
2.2k
Rubyでやりたい駆動開発 / Ruby driven development
chobishiba
1
730
型で語るカタ
irof
0
130
状態遷移図を書こう / Sequence Chart vs State Diagram
orgachem
PRO
1
120
イベントストーミング図からコードへの変換手順 / Procedure for Converting Event Storming Diagrams to Code
nrslib
2
850
PHPで始める振る舞い駆動開発(Behaviour-Driven Development)
ohmori_yusuke
2
400
Featured
See All Featured
A Tale of Four Properties
chriscoyier
160
23k
GraphQLとの向き合い方2022年版
quramy
49
14k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
6
310
Statistics for Hackers
jakevdp
799
220k
Done Done
chrislema
184
16k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.4k
Six Lessons from altMBA
skipperchong
28
3.9k
A Modern Web Designer's Workflow
chriscoyier
695
190k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.8k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
50
5.5k
Making Projects Easy
brettharned
116
6.3k
Raft: Consensus for Rubyists
vanstee
140
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について