Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
ASGIについて
Search
Kaito
May 07, 2021
Programming
0
310
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
Context is King? 〜Verifiability時代とコンテキスト設計 / Beyond "Context is King"
rkaga
9
1.1k
宅宅自以為的浪漫:跟 AI 一起為自己辦的研討會寫一個售票系統
eddie
0
500
手軽に積ん読を増やすには?/読みたい本と付き合うには?
o0h
PRO
1
170
Rubyで鍛える仕組み化プロヂュース力
muryoimpl
0
110
Developing static sites with Ruby
okuramasafumi
0
280
AIエンジニアリングのご紹介 / Introduction to AI Engineering
rkaga
6
2.1k
S3 VectorsとStrands Agentsを利用したAgentic RAGシステムの構築
tosuri13
6
310
著者と進める!『AIと個人開発したくなったらまずCursorで要件定義だ!』
yasunacoffee
0
130
手が足りない!兼業データエンジニアに必要だったアーキテクチャと立ち回り
zinkosuke
0
660
新卒エンジニアのプルリクエスト with AI駆動
fukunaga2025
0
220
UIデザインに役立つ 2025年の最新CSS / The Latest CSS for UI Design 2025
clockmaker
18
7.4k
バックエンドエンジニアによる Amebaブログ K8s 基盤への CronJobの導入・運用経験
sunabig
0
160
Featured
See All Featured
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.6k
Context Engineering - Making Every Token Count
addyosmani
9
510
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.8k
A Modern Web Designer's Workflow
chriscoyier
698
190k
Designing Experiences People Love
moore
143
24k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.1k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
7.9k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.2k
Raft: Consensus for Rubyists
vanstee
141
7.2k
Being A Developer After 40
akosma
91
590k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
51k
Writing Fast Ruby
sferik
630
62k
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について