0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[1分でわかる]ASGIってなんだ?

Posted at

一言で言うと

WSGIの進化版。非同期処理とWebSocketに対応した「新しい共通ルール」。


ASGIとは

ASGI = Asynchronous Server Gateway Interface
(アズギーと読む)

WSGIの後継として、Pythonの非同期処理(async/await)に
対応したWebアプリとサーバーの接続仕様。

WSGIとASGIの違い

┌─────────────────────────────────────────────────────────┐
│                                                         │
│  【WSGI】同期処理                                       │
│                                                         │
│  リクエスト1 → [処理中...] → 完了                      │
│                    ↓                                   │
│  リクエスト2 → [待機...] → [処理中...] → 完了         │
│                              ↓                        │
│  リクエスト3 → [待機...] → [待機...] → [処理中...]    │
│                                                         │
│  → 1つずつ順番に処理(行列に並ぶイメージ)            │
│                                                         │
└─────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────┐
│                                                         │
│  【ASGI】非同期処理                                     │
│                                                         │
│  リクエスト1 → [処理中...]                             │
│  リクエスト2 → [処理中...]  ← 同時に処理できる        │
│  リクエスト3 → [処理中...]                             │
│                                                         │
│  → 待ち時間に他の処理を進める(並行処理)             │
│                                                         │
└─────────────────────────────────────────────────────────┘

ASGIでできるようになったこと

┌─────────────────────────────────────────────────────────┐
│                                                         │
│  ① WebSocket対応                                       │
│     → チャット、リアルタイム通知、ゲーム               │
│                                                         │
│  ② HTTP/2対応                                          │
│     → 高速な通信プロトコル                             │
│                                                         │
│  ③ 大量の同時接続                                      │
│     → 数千〜数万の同時接続を効率的に処理              │
│                                                         │
│  ④ Server-Sent Events(SSE)                           │
│     → サーバーからのプッシュ通知                       │
│                                                         │
│  ⑤ 長時間接続                                          │
│     → 接続を維持したままデータをやり取り              │
│                                                         │
└─────────────────────────────────────────────────────────┘

ASGIサーバーの例

┌─────────────────────────────────────────────────────────┐
│                                                         │
│  Uvicorn(ユビコーン)                                  │
│  → 最もポピュラー、FastAPIの推奨サーバー              │
│  → 軽量・高速                                          │
│                                                         │
│  Daphne(ダフネ)                                       │
│  → Django Channelsの公式サーバー                       │
│                                                         │
│  Hypercorn(ハイパーコーン)                            │
│  → HTTP/2、HTTP/3対応                                  │
│                                                         │
└─────────────────────────────────────────────────────────┘

対応フレームワーク

┌─────────────────────────────────────────────────────────┐
│                                                         │
│  FastAPI                                                │
│  → ASGI専用、最も人気の非同期フレームワーク           │
│                                                         │
│  Starlette                                              │
│  → FastAPIの土台、軽量ASGIフレームワーク              │
│                                                         │
│  Django(3.0以降)                                      │
│  → ASGI対応、Django Channelsでフル活用                │
│                                                         │
│  Quart                                                  │
│  → FlaskのASGI版(asyncio対応Flask)                  │
│                                                         │
└─────────────────────────────────────────────────────────┘

最小のASGIアプリ

async def application(scope, receive, send):
    """
    scope: 接続情報(URL、メソッド、プロトコル等)
    receive: クライアントからデータを受け取る関数
    send: クライアントにデータを送る関数
    """
    await send({
        'type': 'http.response.start',
        'status': 200,
        'headers': [[b'content-type', b'text/plain']],
    })
    await send({
        'type': 'http.response.body',
        'body': b'Hello, ASGI!',
    })

async/await を使った非同期関数になっている。


語呂合わせ

「ASGI = 非同期(Async)でスゴイ」

WSGIに「A(Async)」が付いて、非同期でスゴくなった。


WSGIとASGIの比較表

┌──────────────┬─────────────────┬─────────────────┐
│              │ WSGI            │ ASGI            │
├──────────────┼─────────────────┼─────────────────┤
│ 処理方式     │ 同期            │ 非同期          │
├──────────────┼─────────────────┼─────────────────┤
│ WebSocket    │ ×              │ ◎              │
├──────────────┼─────────────────┼─────────────────┤
│ 同時接続     │ 苦手            │ 得意            │
├──────────────┼─────────────────┼─────────────────┤
│ 代表サーバー │ Gunicorn        │ Uvicorn         │
├──────────────┼─────────────────┼─────────────────┤
│ 代表FW       │ Flask, Django   │ FastAPI         │
├──────────────┼─────────────────┼─────────────────┤
│ 学習コスト   │ 低い            │ やや高い        │
├──────────────┼─────────────────┼─────────────────┤
│ 登場年       │ 2003年          │ 2016年          │
└──────────────┴─────────────────┴─────────────────┘

どっちを使う?

【WSGIを選ぶ場合】
・既存のFlask/Djangoアプリを動かしたい
・シンプルなWebアプリ
・非同期処理が不要

【ASGIを選ぶ場合】
・リアルタイム通信(チャット、通知)が必要
・大量の同時接続を処理したい
・FastAPIを使いたい
・WebSocketを使いたい

まとめ

項目 内容
正式名称 Asynchronous Server Gateway Interface
役割 WSGIの非同期対応版
代表的なサーバー Uvicorn, Daphne, Hypercorn
対応フレームワーク FastAPI, Starlette, Django Channels
強み WebSocket、大量同時接続、非同期処理

1分で理解シリーズ、次回は「cron」

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?