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分でわかる]Gunicornってなんだ?

0
Posted at

一言で言うと

PythonのWebアプリを本番環境で動かすための「WSGIサーバー」。


Gunicornとは

Gunicorn = Green Unicorn(グリーン・ユニコーン)
読み方:グニコーン / ガニコーン

Rubyの「Unicorn」サーバーにインスパイアされた
Python用のWSGI HTTPサーバー。

Flask、DjangoなどのPythonアプリを
本番環境で安定して動かすために使う。

なぜGunicornが必要?

┌─────────────────────────────────────────────────────────┐
│                                                         │
│  【開発環境】                                           │
│  flask run や python manage.py runserver              │
│  → 1リクエストずつしか処理できない                    │
│  → 本番環境では使えない                               │
│                                                         │
│  【本番環境】                                           │
│  Gunicorn                                               │
│  → 複数のワーカーで同時にリクエストを処理             │
│  → 安定性・パフォーマンスが高い                       │
│                                                         │
└─────────────────────────────────────────────────────────┘

図解:Gunicornの立ち位置

┌──────────┐     ┌──────────┐     ┌──────────────┐     ┌──────────┐
│          │     │          │     │              │     │          │
│ ブラウザ │────→│  nginx   │────→│  Gunicorn   │────→│  Flask   │
│          │     │ (リバース│     │  (WSGI)     │     │  Django  │
│          │←────│  プロキシ)│←────│              │←────│          │
│          │     │          │     │              │     │          │
└──────────┘     └──────────┘     └──────────────┘     └──────────┘
                                         │
                                    ┌────┴────┐
                                    │ワーカー1│
                                    │ワーカー2│
                                    │ワーカー3│
                                    └─────────┘

インストールと基本的な使い方

# インストール
pip install gunicorn

# 基本的な起動(app.pyのappオブジェクト)
gunicorn app:app

# ワーカー数を指定(CPUコア数 × 2 + 1 が目安)
gunicorn -w 4 app:app

# ポートを指定
gunicorn -b 0.0.0.0:8000 app:app

# バックグラウンドで起動(デーモン化)
gunicorn -D -w 4 -b 0.0.0.0:8000 app:app

Flaskでの使用例

# app.py
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello, World!'

# 開発時:flask run
# 本番時:gunicorn app:app
# Gunicornで起動
gunicorn -w 4 -b 0.0.0.0:8000 app:app

Djangoでの使用例

# プロジェクト名が myproject の場合
gunicorn myproject.wsgi:application -w 4 -b 0.0.0.0:8000

よく使うオプション

┌──────────────────┬────────────────────────────────────────┐
│ オプション       │ 説明                                    │
├──────────────────┼────────────────────────────────────────┤
│ -w, --workers    │ ワーカー数(並列処理数)               │
├──────────────────┼────────────────────────────────────────┤
│ -b, --bind       │ バインドするアドレス:ポート            │
├──────────────────┼────────────────────────────────────────┤
│ -D, --daemon     │ バックグラウンドで実行                 │
├──────────────────┼────────────────────────────────────────┤
│ --timeout        │ ワーカーのタイムアウト秒数(デフォルト30)│
├──────────────────┼────────────────────────────────────────┤
│ --reload         │ コード変更時に自動リロード(開発用)   │
├──────────────────┼────────────────────────────────────────┤
│ --access-logfile │ アクセスログの出力先                   │
├──────────────────┼────────────────────────────────────────┤
│ --error-logfile  │ エラーログの出力先                     │
└──────────────────┴────────────────────────────────────────┘

語呂合わせ

「Gunicorn = グニっとコーン(角)で突き進む」

ユニコーン(一角獣)のように、Pythonアプリを力強く動かす。


メリット・デメリット

┌─────────────────────────────────────────────────────────┐
│ 【メリット】                                            │
├─────────────────────────────────────────────────────────┤
│ ・シンプルで使いやすい                                 │
│ ・設定が簡単(コマンドラインで完結)                   │
│ ・安定性が高い                                         │
│ ・Pythonで書かれていて拡張しやすい                     │
│ ・ドキュメントが充実                                   │
└─────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────┐
│ 【デメリット】                                          │
├─────────────────────────────────────────────────────────┤
│ ・Windowsでは動作しない(Linux/Mac専用)               │
│ ・静的ファイル配信は苦手(nginxに任せる)              │
│ ・非同期処理は別のワーカークラスが必要                 │
└─────────────────────────────────────────────────────────┘

まとめ

項目 内容
正式名称 Green Unicorn
役割 Python WSGIサーバー
対応フレームワーク Flask, Django, Bottle など
対応OS Linux, Mac(Windowsは非対応)
推奨ワーカー数 CPUコア数 × 2 + 1
組み合わせ nginx + Gunicorn が定番

次回は「nginx」─ Gunicornの前に置くリバースプロキシ

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?