Skip to content
Myr

update docs about gevent/eventlet/greenlet

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parent 1b552d0 · Browse files · 1 file+145 added−36 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+145 added−36 removed

  • other:+145 added−36 removed

1 file+145 added−36 removed

modified:docs/deploying/wsgi-standalone.rst+145 added-36 removed
1Standalone WSGI Containers2==========================1Standalone WSGI Servers2=======================34There are popular servers written in Python that contain WSGI applications and5serve HTTP. These servers stand alone when they run; you can proxy to them6from your web server. Note the section on :ref:`deploying-proxy-setups` if you7run into issues.4Most WSGI servers also provide HTTP servers, so they can run a WSGI5application and make it available externally.67It may still be a good idea to run the server behind a dedicated HTTP8server such as Apache or Nginx. See :ref:`deploying-proxy-setups` if you9run into issues with that.101112Gunicorn13--------1412`Gunicorn`_ 'Green Unicorn' is a WSGI HTTP Server for UNIX. It's a pre-fork13worker model ported from Ruby's Unicorn project. It supports both `eventlet`_14and `greenlet`_. Running a Flask application on this server is quite simple::15`Gunicorn`_ is a WSGI and HTTP server for UNIX. To run a Flask16application, tell Gunicorn how to import your Flask app object.1716 $ gunicorn myproject:app18.. code-block:: text1918`Gunicorn`_ provides many command-line options -- see ``gunicorn -h``.19For example, to run a Flask application with 4 worker processes (``-w204``) binding to localhost port 4000 (``-b 127.0.0.1:4000``)::20 $ gunicorn -w 4 -b 0.0.0.0:5000 your_project:app2122 $ gunicorn -w 4 -b 127.0.0.1:4000 myproject:app22The ``-w 4`` option uses 4 workers to handle 4 requests at once. The23``-b 0.0.0.0:5000`` serves the application on all interfaces on port245000.2524The ``gunicorn`` command expects the names of your application module or25package and the application instance within the module. If you use the26application factory pattern, you can pass a call to that::26Gunicorn provides many options for configuring the server, either27through a configuration file or with command line options. Use28``gunicorn --help`` or see the docs for more information.2928 $ gunicorn "myproject:create_app()"30The command expects the name of your module or package to import and31the application instance within the module. If you use the application32factory pattern, you can pass a call to that.3334.. code-block:: text3536 $ gunicorn -w 4 -b 0.0.0.0:5000 "myproject:create_app()"373839Async with Gevent or Eventlet40~~~~~~~~~~~~~~~~~~~~~~~~~~~~~4142The default sync worker is appropriate for many use cases. If you need43asynchronous support, Gunicorn provides workers using either `gevent`_44or `eventlet`_. This is not the same as Python's ``async/await``, or the45ASGI server spec.4647When using either gevent or eventlet, greenlet>=1.0 is required,48otherwise context locals such as ``request`` will not work as expected.49When using PyPy, PyPy>=7.3.7 is required.5051To use gevent:5253.. code-block:: text5455 $ gunicorn -k gevent -b 0.0.0.0:5000 your_project:app5657To use eventlet:5859.. code-block:: text6061 $ gunicorn -k eventlet -b 0.0.0.0:5000 your_project:app626364.. _Gunicorn: https://gunicorn.org/65.. _gevent: http://www.gevent.org/66.. _eventlet: https://eventlet.net/67.. _greenlet: https://greenlet.readthedocs.io/en/latest/686970uWSGI35--------71-----7237`uWSGI`_ is a fast application server written in C. It is very configurable38which makes it more complicated to setup than gunicorn.73`uWSGI`_ is a fast application server written in C. It is very74configurable, which makes it more complicated to setup than Gunicorn.75It also provides many other utilities for writing robust web76applications. To run a Flask application, tell Gunicorn how to import77your Flask app object.7840Running `uWSGI HTTP Router`_::79.. code-block:: text8042 $ uwsgi --http 127.0.0.1:5000 --module myproject:app81 $ uwsgi --master -p 4 --http 0.0.0.0:5000 -w your_project:app8244For a more optimized setup, see :doc:`configuring uWSGI and NGINX <uwsgi>`.83The ``-p 4`` option uses 4 workers to handle 4 requests at once. The84``--http 0.0.0.0:5000`` serves the application on all interfaces on port855000.8687uWSGI has optimized integration with Nginx and Apache instead of using88a standard HTTP proxy. See :doc:`configuring uWSGI and Nginx <uwsgi>`.899091Async with Gevent92~~~~~~~~~~~~~~~~~9394The default sync worker is appropriate for many use cases. If you need95asynchronous support, uWSGI provides workers using `gevent`_. It also96supports other async modes, see the docs for more information. This is97not the same as Python's ``async/await``, or the ASGI server spec.9899When using gevent, greenlet>=1.0 is required, otherwise context locals100such as ``request`` will not work as expected. When using PyPy,101PyPy>=7.3.7 is required.102103.. code-block:: text104105 $ uwsgi --master --gevent 100 --http 0.0.0.0:5000 -w your_project:app106107.. _uWSGI: https://uwsgi-docs.readthedocs.io/en/latest/47.. _uWSGI HTTP Router: https://uwsgi-docs.readthedocs.io/en/latest/HTTP.html#the-uwsgi-http-https-router108109110Gevent50-------111------11252`Gevent`_ is a coroutine-based Python networking library that uses53`greenlet`_ to provide a high-level synchronous API on top of `libev`_54event loop::113Prefer using `Gunicorn`_ with Gevent workers rather than using Gevent114directly. Gunicorn provides a much more configurable and115production-tested server. See the section on Gunicorn above.116117`Gevent`_ allows writing asynchronous, coroutine-based code that looks118like standard synchronous Python. It uses `greenlet`_ to enable task119switching without writing ``async/await`` or using ``asyncio``.120121It provides a WSGI server that can handle many connections at once122instead of one per worker process.123124`Eventlet`_, described below, is another library that does the same125thing. Certain dependencies you have, or other consideration, may affect126which of the two you choose to use127128To use gevent to serve your application, import its ``WSGIServer`` and129use it to run your ``app``.130131.. code-block:: python132133 from gevent.pywsgi import WSGIServer57 from yourapplication import app134 from your_project import app13559 http_server = WSGIServer(('', 5000), app)136 http_server = WSGIServer(("", 5000), app)137 http_server.serve_forever()13862.. _Gevent: http://www.gevent.org/63.. _greenlet: https://greenlet.readthedocs.io/en/latest/64.. _libev: http://software.schmorp.de/pkg/libev.html139140Eventlet141--------142143Prefer using `Gunicorn`_ with Eventlet workers rather than using144Eventlet directly. Gunicorn provides a much more configurable and145production-tested server. See the section on Gunicorn above.146147`Eventlet`_ allows writing asynchronous, coroutine-based code that looks148like standard synchronous Python. It uses `greenlet`_ to enable task149switching without writing ``async/await`` or using ``asyncio``.150151It provides a WSGI server that can handle many connections at once152instead of one per worker process.153154`Gevent`_, described above, is another library that does the same155thing. Certain dependencies you have, or other consideration, may affect156which of the two you choose to use157158To use eventlet to serve your application, import its ``wsgi.server``159and use it to run your ``app``.160161.. code-block:: python162163 import eventlet164 from eventlet import wsgi165 from your_project import app166167 wsgi.server(eventlet.listen(("", 5000), app)168169170Twisted Web171-----------172173`Twisted Web`_ is the web server shipped with `Twisted`_, a mature,174non-blocking event-driven networking library. Twisted Web comes with a175standard WSGI container which can be controlled from the command line using72the ``twistd`` utility::176the ``twistd`` utility:177178.. code-block:: text179180 $ twistd web --wsgi myproject.app181182This example will run a Flask application called ``app`` from a module named183``myproject``.184185Twisted Web supports many flags and options, and the ``twistd`` utility does186as well; see ``twistd -h`` and ``twistd web -h`` for more information. For187example, to run a Twisted Web server in the foreground, on port 8080, with an82application from ``myproject``::188application from ``myproject``:189190.. code-block:: text191192 $ twistd -n web --port tcp:8080 --wsgi myproject.app193194.. _Twisted: https://twistedmatrix.com/trac/195.. _Twisted Web: https://twistedmatrix.com/trac/wiki/TwistedWeb196197198.. _deploying-proxy-setups:199200Proxy Setups201------------
61 unmodified lines