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
300
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
私はどうやって技術力を上げたのか
yusukebe
43
18k
AI Coding Meetup #3 - 導入セッション / ai-coding-meetup-3
izumin5210
0
3.3k
オープンソースソフトウェアへの解像度🔬
utam0k
15
2.8k
Go言語はstack overflowの夢を見るか?
logica0419
0
270
Leading Effective Engineering Teams in the AI Era
addyosmani
4
380
Writing Better Go: Lessons from 10 Code Reviews
konradreiche
0
1.2k
monorepo の Go テストをはやくした〜い!~最小の依存解決への道のり~ / faster-testing-of-monorepos
convto
2
490
Swift Concurrency - 状態監視の罠
objectiveaudio
2
520
CSC509 Lecture 06
javiergs
PRO
0
260
Server Side Kotlin Meetup vol.16: 内部動作を理解して ハイパフォーマンスなサーバサイド Kotlin アプリケーションを書こう
ternbusty
3
180
高度なUI/UXこそHotwireで作ろう Kaigi on Rails 2025
naofumi
4
4k
Pull-Requestの内容を1クリックで動作確認可能にするワークフロー
natmark
2
510
Featured
See All Featured
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
GitHub's CSS Performance
jonrohan
1032
470k
Designing for humans not robots
tammielis
254
26k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
980
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
A Tale of Four Properties
chriscoyier
161
23k
Into the Great Unknown - MozCon
thekraken
40
2.1k
Six Lessons from altMBA
skipperchong
28
4k
Raft: Consensus for Rubyists
vanstee
139
7.1k
Building Better People: How to give real-time feedback that sticks.
wjessup
369
20k
We Have a Design System, Now What?
morganepeng
53
7.8k
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について