Skip to Content
ファストAPI
book

ファストAPI

by Bill Lubanovic
March 2025
Intermediate to advanced
280 pages
3h 20m
Japanese
O'Reilly Media, Inc.
Content preview from ファストAPI

第13章. 生産

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

プログラマがプログラムを書くように建築家が建物を建てたら、最初にやってきたキツツキが文明を破壊するだろう。

ジェラルド・ワインバーグ(コンピュータ科学者

プレビュー

この章では、アプリケーションを本番稼動させ、正しく効率的に稼動させ続ける方法について、多くのシナリオを紹介する。非常に詳細な内容もあるので、場合によっては、ここに詰め込むのではなく、役に立つ外部のドキュメントを参照することにする。

デプロイ

本書で紹介する のコード例では、localhost 、ポート8000 で動作するuvicornのインスタンスを1つだけ使用している。 多くのトラフィックを処理するためには、最新のハードウェアが提供するマルチコアで動作する複数のサーバが必要である。 また、これらのサーバの上位に、以下を実行するものが必要である:

  • 運営を続ける(監督者)

  • 外部からのリクエストを集めて送り込む(リバースプロキシ)

  • レスポンスを返す

  • HTTPS終端(SSL 復号化)を提供する。

複数のワーカー

おそらくGunicornという別のPythonサーバを見たことがあるだろう。これは複数のワーカーを監視できるが、WSGIサーバであり、FastAPIはASGIをベースにしている。 幸運なことに、Gunicornで管理できる特殊化Uvicornワーカークラスがある。

例13-1は、これらのUvicornワーカーをlocalhost 、ポート8000(これは公式ドキュメントからの引用である)にセットアップする。引用符は、シェルが特殊化しないように保護する。

例 13-1. UvicornワーカーでGunicornを使う
$ pip install "uvicorn[standard]" gunicorn
$ gunicorn main:app --workers 4 --worker-class \
uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000

8000 Gunicornがあなたの命令を実行するとき、多くの行が表示される。トップレベルのGunicornプロセスを起動し、4つのUvicornワーカーサブプロセスと話し、すべてのポートlocalhost (0.0.0.0) を共有する。他に何か必要であれば、ホスト、ポート、ワーカーの数を変更する。main:appmain.pyと変数名app のFastAPIオブジェクトを参照する。Gunicornのドキュメントには次のように書かれている:

Gunicornは、毎秒数百、数千のリクエストを処理するのに4-12ワーカー・プロセスしか必要としないはずだ。

例13-2のように、Uvicorn自身も複数のUvicornワーカーを起動できることがわかった。

例 13-2. UvicornとUvicornワーカーを使う
$ uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4

しかし、このメソッドではプロセス管理ができないので、通常はgunicornのメソッドが好まれる。 Uvicornには他にもプロセスマネージャーが存在する:公式ドキュメントを参照のこと。

これは前節で述べた4つのジョブのうち3つを処理するが、HTTPS暗号化は処理しない。

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

GitHubアクションを学ぶ

GitHubアクションを学ぶ

Brent Laster
仕事ではじめる機械学習 第2版

仕事ではじめる機械学習 第2版

有賀 康顕, 中山 心太, 西林 孝

Publisher Resources

ISBN: 9798341624658