eventlet

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

eventletは非同期処理の、コルーチンを土台にしたコードを標準の同期処理をするPythonのような見た目で書けるようにします。greenletを使うことで、async/awaitを書いたり、asincioを使ったりせずに、タスクの切り替えをできるようにします。 `eventlet`_ 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``.

geventは、同じことをするもう一つのライブラリです。ある種の依存対象や、その他の検討事項が、2つのどちらを使うかの選択に影響を与えるかもしれません。 :doc:`gevent` 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.

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

インストール Installing

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

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

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

実行 Running

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

wsgi.py
import eventlet
from eventlet import wsgi
from hello import create_app

app = create_app()
wsgi.server(eventlet.listen(("127.0.0.1", 8000), app)
$ python wsgi.py
(x) wsgi starting up on http://127.0.0.1:8000

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

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

外部からアクセスできる全ての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.