Gunicorn

GunicornはPythonだけで実装されたWSGIサーバで、シンプルな設定と、性能チューニングのための複数のworker実装が使えます。 `Gunicorn`_ is a pure Python WSGI server with simple configuration and multiple worker implementations for performance tuning.

  • ホスティング・サービスと容易に統合できることが多いです。 It tends to integrate easily with hosting platforms.

  • Windowsはサポートしません(しかし、WSL上で走ります)。 It does not support Windows (but does run on WSL).

  • 追加の依存対象やコンパイル処理が不要であるため、インストールが容易です。 It is easy to install as it does not require additional dependencies or compilation.

  • geventまたはeventletを使った、組み込みのasync(非同期処理の)workerのサポートがあります。 It has built-in async worker support using gevent or eventlet.

このページはGunicornを実行する基本について説明します。そのdocumentationを読みgunicorn --helpを使って、どの目玉機能(feature)が利用できるか理解しておいてください。 This page outlines the basics of running Gunicorn. Be sure to read its `documentation`_ and use ``gunicorn --help`` to understand what features are available.

インストール Installing

追加の依存対象やコンパイル処理が不要であるため、Gunicornは容易にインストールできます。GunicornはWindowsではただWSLのもとでだけ走ります。 Gunicorn is easy to install, as it does not require external dependencies or compilation. It runs on Windows only under WSL.

virtualenvを作成し、アプリケーションをインストールし、それからgunicornをインストールします。 Create a virtualenv, install your application, then install ``gunicorn``.

$ cd hello-app
$ python -m venv venv
$ . venv/bin/activate
$ pip install .  # install your application
$ pip install gunicorn

実行 Running

Gunicornのただ一つ必要な引数は、Flaskアプリケーションをどのように読み込むかをGunicornに伝えます。記法は{module_import}:{app_variable}({importするモジュール}:{Flaskアプリを読み込む変数})です。module_importは、自分のアプリケーションがあるモジュールの、ドット(「.」)で分けられたimport名です。app_variableはアプリケーションが設定されている変数です。もしapp factoryパターンを使っている場合、app_variableは(どのような引数を伴った)関数呼び出しでも可能です。 The only required argument to Gunicorn tells it how to load your Flask application. The syntax is ``{module_import}:{app_variable}``. ``module_import`` is the dotted import name to the module with your application. ``app_variable`` is the variable with the application. It can also be a function call (with any arguments) if you're using the app factory pattern.

# equivalent to 'from hello import app'
$ gunicorn -w 4 'hello:app'

# equivalent to 'from hello import create_app; create_app()'
$ gunicorn -w 4 'hello:create_app()'

Starting gunicorn 20.1.0
Listening at: http://127.0.0.1:8000 (x)
Using worker: sync
Booting worker with pid: x
Booting worker with pid: x
Booting worker with pid: x
Booting worker with pid: x

-wオプションは走らせるプロセス数を指定します; 開始時の値はCPU * 2にできるでしょう。標準設定ではただ1つのworkerになり、それはおそらくあなたが標準設定のworkerのタイプでは望まないものでしょう。 The ``-w`` option specifies the number of processes to run; a starting value could be ``CPU * 2``. The default is only 1 worker, which is probably not what you want for the default worker type.

各リクエストのログは標準設定では表示されず、ただworkerのinfoとerrorのログだけが表示されます。アクセスログを標準出力に表示させるには、--access-logfile=-オプションを使います。 Logs for each request aren't shown by default, only worker info and errors are shown. To show access logs on stdout, use the ``--access-logfile=-`` option.

外部への結び付け(Binding Externally) Binding Externally

アプリケーションのコードがrootとして走るようになり、安全ではなくなるため、Gunicornはrootとして走らせないようにするべきです。しかしながら、これはポート80や443に結びつけ(bind)出来なくなることを意味します(訳注: 一般的なLinuxでは、およそ1000より小さなポート番号は、セキュリティ対策のためroot権限を持たないプログラムではbindできないようになっており、従ってroot権限がないWSGIサーバではHTTPやHTTPSで標準的に使われる80や443番ポートでリクエストを受け付けられないことになります)。代わりに、nginxまたはApache httpdのようなリバースプロキシをGunicornの前面に使うべきです。 Gunicorn should not be run as root because it would cause your application code to run as root, which is not secure. However, this means it will not be possible to bind to port 80 or 443. Instead, a reverse proxy such as :doc:`nginx` or :doc:`apache-httpd` should be used in front of Gunicorn.

外部からアクセスできる全てのIPアドレスのroot権限不要なポートは、-b 0.0.0.0オプションを使って結び付け(bind)できます。これは、リバースプロキシを用意しているときは行わないでください、さもなければリバースプロキシを迂回することが可能になります。 You can bind to all external IPs on a non-privileged port using the ``-b 0.0.0.0`` option. Don't do this when using a reverse proxy setup, otherwise it will be possible to bypass the proxy.

$ gunicorn -w 4 -b 0.0.0.0 'hello:create_app()'
Listening at: http://0.0.0.0:8000 (x)

0.0.0.0はアクセスするには適切なアドレスではないため、ブラウザでは具体的なIPアドレスを使うべきです。 ``0.0.0.0`` is not a valid address to navigate to, you'd use a specific IP address in your browser.

geventまたはeventletを使った非同期処理 Async with gevent or eventlet

標準設定の同期処理するworkerは、おおくの使用状況で適切ではありません。もし非同期処理するもののサポートが必要な場合、Gunicornはgeventまたはeventletのいずれかを使用するworkerを提供します。これはPythonのasync/await、またはASGIサーバの仕様と同じものではありません。これらのworkerを使用して得られるいかなる利点も見るためには、自分のコードの中で実際にgevent/eventletを使用する必要があります。 The default sync worker is appropriate for many use cases. If you need asynchronous support, Gunicorn provides workers using either `gevent`_ or `eventlet`_. This is not the same as Python's ``async/await``, or the ASGI server spec. You must actually use gevent/eventlet in your own code to see any benefit to using the workers.

geventまたはeventletのいずれかを使うときは、greenlet>=1.0が必要であり、さもなければrequestのようなコンテキストの局所領域(context local)は期待通りに働かないでしょう。PyPyを使うときは、PyPy>=7.3.7が必要です。 When using either gevent or eventlet, greenlet>=1.0 is required, otherwise context locals such as ``request`` will not work as expected. When using PyPy, PyPy>=7.3.7 is required.

geventの使用: To use gevent:

$ gunicorn -k gevent 'hello:create_app()'
Starting gunicorn 20.1.0
Listening at: http://127.0.0.1:8000 (x)
Using worker: gevent
Booting worker with pid: x

eventletの使用: To use eventlet:

$ gunicorn -k eventlet 'hello:create_app()'
Starting gunicorn 20.1.0
Listening at: http://127.0.0.1:8000 (x)
Using worker: eventlet
Booting worker with pid: x