GunicornでFlaskを実行する
複数のワーカーでアプリを配信します
「GunicornでFlaskを実行する」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
What Gunicorn Is
Gunicorn is a production WSGI server for Python. It runs your Flask app across multiple worker processes so many users can be served at once. 🦄
Install It
Add Gunicorn to your environment with pip. Treat it as a real dependency and pin it in your requirements file.
pip install gunicornIt Needs Your App Object
Gunicorn must find the WSGI callable. That is the Flask app instance you created, usually named app in your module.
# app.py
from flask import Flask
app = Flask(__name__)The module:variable Pattern
You point Gunicorn at your app using module:variable syntax. The part before the colon is the file, the part after is the app name.
gunicorn app:appA Clean wsgi.py Entry Point
Many teams add a tiny wsgi.py that just exposes the app. It keeps the run command stable even as the app grows.
# wsgi.py
from app import app
# gunicorn wsgi:appChoosing the Worker Count
The --workers flag sets how many processes handle requests. A common starting point is two times your CPU cores plus one.
gunicorn --workers 3 wsgi:appBinding to an Address
Use --bind to choose the host and port. Binding to 0.0.0.0 lets the app accept traffic from outside the container.
gunicorn --bind 0.0.0.0:8000 wsgi:appApp Factory Friendly
Using create_app? Gunicorn can call your factory directly with a function call in the target string.
gunicorn "wsgi:create_app()"Timeouts Keep You Safe
The --timeout flag kills a worker stuck on a slow request. It stops one bad request from freezing the whole server.
gunicorn --timeout 30 wsgi:appNo More Reloader
Gunicorn does not auto-reload on code changes in production. You restart it on deploy, which gives you a predictable release moment.
Logs You Can Trust
Gunicorn writes structured access logs showing each request and its status. Pipe them to stdout so your platform can collect them.
Quick Check
How do you tell Gunicorn which app to run?
Recap
Gunicorn runs your Flask app with real workers, a bind address, and timeouts. Next you will lock all of this into a Docker image. 📦
よくある質問
「GunicornでFlaskを実行する」レッスンは無料ですか?
はい。「GunicornでFlaskを実行する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「GunicornでFlaskを実行する」で何を学びますか?
複数のワーカーでアプリを配信します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「GunicornでFlaskを実行する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 組み込み開発サーバーを使わない理由
- GunicornでFlaskを実行する
- 本番用Dockerfileを書く
- NginxとComposeを前段に置く