以下の内容はhttps://nikkie-ftnext.hatenablog.com/entry/fastapi-can-mount-wsgi-application-with-a2wsgiより取得しました。


a2wsgi を使って FastAPI に WSGI アプリケーションを mount する

はじめに

七尾百合子さん、お誕生日 346日目 おめでとうございます! nikkieです。

FastAPI について Today(※最近) I Learned です。

目次

FastAPI ドキュメント「Including WSGI」

fastapi.tiangolo.com

FastAPI、お前、WSGI アプリケーション(例:Flask)を mount できたのか...!!

# コードの全容は上記のドキュメントをご確認ください
from a2wsgi import WSGIMiddleware

app = FastAPI()
app.mount("/v1", WSGIMiddleware(flask_app))

WSGI と ASGI

  • WSGI: Web Server Gateway Interface1
  • ASGI: Asynchronous Server Gateway Interface

この2つの関係性については、みんなのPython勉強会から rhoboro さんの説明が分かりやすいと思います

スライド46:ASGI 登場の背景
HTTP/2 や WebSocket(Web の世界の複雑化)

リクエストとレスポンスが同期的な 1:1 の関係ではなく、従来の WSGI では対応できない

WSGI の課題を解決したのが ASGI と私は理解していました。

今回知ったことは、この2つには互換性があったということです!
rhoboro さんに教えていただいた2
https://asgi.readthedocs.io/en/latest/introduction.html#wsgi-compatibility

ASGI is also designed to be a superset of WSGI, and there’s a defined way of translating between the two, allowing WSGI applications to be run inside ASGI servers through a translation wrapper (provided in the asgiref library).
A threadpool can be used to run the synchronous WSGI applications away from the async event loop.

立役者 a2wsgi

FastAPI のドキュメントに登場するのが a2wsgi

Convert WSGI app to ASGI app or ASGI app to WSGI app.

from a2wsgi import WSGIMiddleware

ASGI_APP = WSGIMiddleware(WSGI_APP)
from a2wsgi import ASGIMiddleware

WSGI_APP = ASGIMiddleware(ASGI_APP)

a2wsgi は rhoboro さんの資料で紹介される「ASGI に準拠したサーバー、アプリケーション、ツールの一覧」(スライド41)にも載っています3
https://asgi.readthedocs.io/en/latest/implementations.html#a2wsgi

Only depend on the standard library.

この a2wsgi、実装は標準ライブラリのみです4
標準ライブラリでこんなことができるの?!
すごい。実装が気になります

Django (WSGI) meets FastAPI (ASGI) ❤️

a2wsgi-examples を参考に手を動かします

Django の例
https://github.com/hasansezertasan/a2wsgi-examples/blob/main/src/applications/django/app.py

uv run app.pyで inline script metadata を利用して起動。
別ターミナルで

% curl localhost:8000/
{"message":"Hello World from FastAPI"}
% curl localhost:8000/django/
{"message": "Hello World from Django"}

おおおおおお!!

終わりに

FastAPI の ASGI アプリケーションと一緒に、過去に作ってきた FastAPI や Django などの WSGI アプリケーションも動かせます。
WSGI と ASGI の世界は繋がっていたことを知りました。
立役者 a2wsgi により、逆に ASGI アプリケーションを WSGI アプリケーションと一緒に動かすこともできそうです。

最後に、なにが TIL につながったかです。
直近の 0.132.0 のリリースで引っかかりました。
https://github.com/fastapi/fastapi/releases/tag/0.132.0

Flask のバージョンを上げる?
FastAPI に Flask という別の Web アプリケーションフレームワークの依存が必要とは思えません。
妙だな...

プルリクエストを追っていったところ(ここは GPT-5.3-Codex 利用)、Flask に言及したドキュメントの存在に気づいたのでした。
FastAPI のドキュメントは生きていてテストの対象にもなっており、そのため Flask をインストールしています。
https://github.com/fastapi/fastapi/blob/0.132.0/tests/test_tutorial/test_wsgi/test_tutorial001.py


  1. 過去記事より
  2. ありがとうございます
  3. PyCon mini 東海 2024 の筒井さんの資料にもありました
  4. https://github.com/abersheeran/a2wsgi/blob/v1.10.10/pyproject.toml#L4



以上の内容はhttps://nikkie-ftnext.hatenablog.com/entry/fastapi-can-mount-wsgi-application-with-a2wsgiより取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14