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
Provenance
- Commit
df806c8- Tree
d007f94- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
1b552d0
1 file changed+145 added−36 removed
modified:docs/deploying/wsgi-standalone.rst+145 added-36 removed
1-Standalone WSGI Containers2-==========================1+Standalone WSGI Servers2+=======================3 4-There are popular servers written in Python that contain WSGI applications and5-serve HTTP. These servers stand alone when they run; you can proxy to them6-from your web server. Note the section on :ref:`deploying-proxy-setups` if you7-run into issues.4+Most WSGI servers also provide HTTP servers, so they can run a WSGI5+application and make it available externally.6 7+It may still be a good idea to run the server behind a dedicated HTTP8+server such as Apache or Nginx. See :ref:`deploying-proxy-setups` if you9+run into issues with that.10+11+12 Gunicorn13 --------14 12-`Gunicorn`_ 'Green Unicorn' is a WSGI HTTP Server for UNIX. It's a pre-fork13-worker model ported from Ruby's Unicorn project. It supports both `eventlet`_14-and `greenlet`_. Running a Flask application on this server is quite simple::15+`Gunicorn`_ is a WSGI and HTTP server for UNIX. To run a Flask16+application, tell Gunicorn how to import your Flask app object.17 16- $ gunicorn myproject:app18+.. code-block:: text19 18-`Gunicorn`_ provides many command-line options -- see ``gunicorn -h``.19-For example, to run a Flask application with 4 worker processes (``-w20-4``) binding to localhost port 4000 (``-b 127.0.0.1:4000``)::20+ $ gunicorn -w 4 -b 0.0.0.0:5000 your_project:app21 22- $ gunicorn -w 4 -b 127.0.0.1:4000 myproject:app22+The ``-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 port24+5000.25 24-The ``gunicorn`` command expects the names of your application module or25-package and the application instance within the module. If you use the26-application factory pattern, you can pass a call to that::26+Gunicorn provides many options for configuring the server, either27+through a configuration file or with command line options. Use28+``gunicorn --help`` or see the docs for more information.29 28- $ gunicorn "myproject:create_app()"30+The command expects the name of your module or package to import and31+the application instance within the module. If you use the application32+factory pattern, you can pass a call to that.33 34+.. code-block:: text35+36+ $ gunicorn -w 4 -b 0.0.0.0:5000 "myproject:create_app()"37+38+39+Async with Gevent or Eventlet40+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~41+42+The default sync worker is appropriate for many use cases. If you need43+asynchronous support, Gunicorn provides workers using either `gevent`_44+or `eventlet`_. This is not the same as Python's ``async/await``, or the45+ASGI server spec.46+47+When using either gevent or eventlet, greenlet>=1.0 is required,48+otherwise context locals such as ``request`` will not work as expected.49+When using PyPy, PyPy>=7.3.7 is required.50+51+To use gevent:52+53+.. code-block:: text54+55+ $ gunicorn -k gevent -b 0.0.0.0:5000 your_project:app56+57+To use eventlet:58+59+.. code-block:: text60+61+ $ gunicorn -k eventlet -b 0.0.0.0:5000 your_project:app62+63+64 .. _Gunicorn: https://gunicorn.org/65+.. _gevent: http://www.gevent.org/66 .. _eventlet: https://eventlet.net/67+.. _greenlet: https://greenlet.readthedocs.io/en/latest/68 69 70 uWSGI35---------71+-----72 37-`uWSGI`_ is a fast application server written in C. It is very configurable38-which makes it more complicated to setup than gunicorn.73+`uWSGI`_ is a fast application server written in C. It is very74+configurable, which makes it more complicated to setup than Gunicorn.75+It also provides many other utilities for writing robust web76+applications. To run a Flask application, tell Gunicorn how to import77+your Flask app object.78 40-Running `uWSGI HTTP Router`_::79+.. code-block:: text80 42- $ uwsgi --http 127.0.0.1:5000 --module myproject:app81+ $ uwsgi --master -p 4 --http 0.0.0.0:5000 -w your_project:app82 44-For a more optimized setup, see :doc:`configuring uWSGI and NGINX <uwsgi>`.83+The ``-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 port85+5000.86 87+uWSGI has optimized integration with Nginx and Apache instead of using88+a standard HTTP proxy. See :doc:`configuring uWSGI and Nginx <uwsgi>`.89+90+91+Async with Gevent92+~~~~~~~~~~~~~~~~~93+94+The default sync worker is appropriate for many use cases. If you need95+asynchronous support, uWSGI provides workers using `gevent`_. It also96+supports other async modes, see the docs for more information. This is97+not the same as Python's ``async/await``, or the ASGI server spec.98+99+When using gevent, greenlet>=1.0 is required, otherwise context locals100+such as ``request`` will not work as expected. When using PyPy,101+PyPy>=7.3.7 is required.102+103+.. code-block:: text104+105+ $ uwsgi --master --gevent 100 --http 0.0.0.0:5000 -w your_project:app106+107 .. _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-router108 109+110 Gevent50--------111+------112 52-`Gevent`_ is a coroutine-based Python networking library that uses53-`greenlet`_ to provide a high-level synchronous API on top of `libev`_54-event loop::113+Prefer using `Gunicorn`_ with Gevent workers rather than using Gevent114+directly. Gunicorn provides a much more configurable and115+production-tested server. See the section on Gunicorn above.116 117+`Gevent`_ allows writing asynchronous, coroutine-based code that looks118+like standard synchronous Python. It uses `greenlet`_ to enable task119+switching without writing ``async/await`` or using ``asyncio``.120+121+It provides a WSGI server that can handle many connections at once122+instead of one per worker process.123+124+`Eventlet`_, described below, is another library that does the same125+thing. Certain dependencies you have, or other consideration, may affect126+which of the two you choose to use127+128+To use gevent to serve your application, import its ``WSGIServer`` and129+use it to run your ``app``.130+131+.. code-block:: python132+133 from gevent.pywsgi import WSGIServer57- from yourapplication import app134+ from your_project import app135 59- http_server = WSGIServer(('', 5000), app)136+ http_server = WSGIServer(("", 5000), app)137 http_server.serve_forever()138 62-.. _Gevent: http://www.gevent.org/63-.. _greenlet: https://greenlet.readthedocs.io/en/latest/64-.. _libev: http://software.schmorp.de/pkg/libev.html139 140+Eventlet141+--------142+143+Prefer using `Gunicorn`_ with Eventlet workers rather than using144+Eventlet directly. Gunicorn provides a much more configurable and145+production-tested server. See the section on Gunicorn above.146+147+`Eventlet`_ allows writing asynchronous, coroutine-based code that looks148+like standard synchronous Python. It uses `greenlet`_ to enable task149+switching without writing ``async/await`` or using ``asyncio``.150+151+It provides a WSGI server that can handle many connections at once152+instead of one per worker process.153+154+`Gevent`_, described above, is another library that does the same155+thing. Certain dependencies you have, or other consideration, may affect156+which of the two you choose to use157+158+To use eventlet to serve your application, import its ``wsgi.server``159+and use it to run your ``app``.160+161+.. code-block:: python162+163+ import eventlet164+ from eventlet import wsgi165+ from your_project import app166+167+ wsgi.server(eventlet.listen(("", 5000), app)168+169+170 Twisted Web171 -----------172 173 `Twisted Web`_ is the web server shipped with `Twisted`_, a mature,174 non-blocking event-driven networking library. Twisted Web comes with a175 standard WSGI container which can be controlled from the command line using72-the ``twistd`` utility::176+the ``twistd`` utility:177 178+.. code-block:: text179+180 $ twistd web --wsgi myproject.app181 182 This example will run a Flask application called ``app`` from a module named183 ``myproject``.184 185 Twisted Web supports many flags and options, and the ``twistd`` utility does186 as well; see ``twistd -h`` and ``twistd web -h`` for more information. For187 example, to run a Twisted Web server in the foreground, on port 8080, with an82-application from ``myproject``::188+application from ``myproject``:189 190+.. code-block:: text191+192 $ twistd -n web --port tcp:8080 --wsgi myproject.app193 194 .. _Twisted: https://twistedmatrix.com/trac/195 .. _Twisted Web: https://twistedmatrix.com/trac/wiki/TwistedWeb196 197+198 .. _deploying-proxy-setups:199 200 Proxy Setups201 ------------61 unmodified lines