gevent

直接geventを使うよりも、GunicornまたはuWSGIでgeventのworkerを使う方が好ましいです。GunicornとuWSGIはより設定可能で製品レベルのテストがされたサーバです。 Prefer using :doc:`gunicorn` or :doc:`uwsgi` with gevent workers rather than using `gevent`_ directly. Gunicorn and uWSGI provide much more configurable and production-tested servers.

geventは非同期処理の、コルーチンを土台にしたコードを標準の同期処理をするPythonのような見た目で書けるようにします。greenletを使うことで、async/awaitを書いたり、asincioを使ったりせずに、タスクの切り替えをできるようにします。 `gevent`_ allows writing asynchronous, coroutine-based code that looks like standard synchronous Python. It uses `greenlet`_ to enable task switching without writing ``async/await`` or using ``asyncio``.

eventletは、同じことをするもう一つのライブラリです。ある種の依存対象や、その他の検討事項が、2つのどちらを使うかの選択に影響を与えるかもしれません。 :doc:`eventlet` is another library that does the same thing. Certain dependencies you have, or other considerations, may affect which of the two you choose to use.

geventは、接続ごとにworkerプロセスを使う代わりに、多くの接続を同時に処理するWSGIサーバを提供します。このサーバを使用して得られるいかなる利点も見るためには、自分のコードの中で実際にgeventを使用する必要があります。 gevent provides a WSGI server that can handle many connections at once instead of one per worker process. You must actually use gevent in your own code to see any benefit to using the server.

インストール Installing

geventを使うときは、greenlet>=1.0が必要であり、さもなければrequestのようなコンテキストの局所領域(context local)は期待通りに働かないでしょう。PyPyを使うときは、PyPy>=7.3.7が必要です。 When using gevent, 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.

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

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

実行 Running

Flaskアプリケーションを提供するためにgeventを使うには、そのWSGIServerをimportするスクリプトを、Flaskアプリのインスタンスであってもapp factoryであっても同様に書きます。 To use gevent to serve your application, write a script that imports its ``WSGIServer``, as well as your app or app factory.

wsgi.py
from gevent.pywsgi import WSGIServer
from hello import create_app

app = create_app()
http_server = WSGIServer(("127.0.0.1", 8000), app)
http_server.serve_forever()
$ python wsgi.py

サーバ開始時は、何も出力されません。 No output is shown when the server starts.

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

アプリケーションのコードがrootとして走るようになり、安全ではなくなるため、geventはrootとして走らせないようにするべきです。しかしながら、これはポート80や443に結びつけ(bind)出来なくなることを意味します(訳注: 一般的なLinuxでは、およそ1000より小さなポート番号は、セキュリティ対策のためroot権限を持たないプログラムではbindできないようになっており、従ってroot権限がないWSGIサーバではHTTPやHTTPSで標準的に使われる80や443番ポートでリクエストを受け付けられないことになります)。代わりに、nginxまたはApache httpdのようなリバースプロキシをgeventの前面に使うべきです。 gevent 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 gevent.

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

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.