Search code examples
pythonpython-3.xflaskgevent

What is the use of a web server in flask deployments?


Note: the question was closed as a duplicate but it is not (of neither the questions). I specifically described an app in web mode, then in WSGI mode - I have no doubts on which to use when (both linked questions address this point). I also explained that I know well what a web server is for. The accepted answer makes a good summary of that, and answers the question of whether a web server is a requirement to run a prod flask app (it is not).

I have a flask application. The development version, with all its limitations, is

import flask

app = flask.Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

app.run()

When I want to move it to production, I use gevent, per the deployment documentation:

import gevent.monkey
gevent.monkey.patch_all()
import flask
import gevent.wsgi

app = flask.Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

gevent.wsgi.WSGIServer(('127.0.0.1', 5000), app).serve_forever()

Both cases work, with the first one occasionally having issues (which is not unexpected, due to its nature).

Then, a lot of information I read here and there mention that the third component for production deployments of a flask application is a web server (Apache, nginx). What is its actual use for WSGI-enabled flask applications?

Specifically, I would like to understand whether it has a practical impact on the performance / stability of the flask application.

I know what the various usages of a webserver are (authentication, reverse proxy, whitelisting, rewrites, load management, and many others). My question is specifically about what a webserver provides for a flask WSGI application (if anything).


Solution

  • With Flask, you build your web application. WSGI is the interface the application follows to be hosted by WSGI servers. gevent is a WSGI server that can host your application.

    Usually, you would then put a full fledged web server (e.g. nginx or apache) as a reverse proxy in front of it to get the full features modern web servers usually offer, without the intermediate WSGI server having to offer those features themselves. That makes everything simpler and easier to reason about, since every component can focus on what it does best (the WSGI server’s job being to only host your application).

    The dev server that comes with Flask is a very simple server that runs in a single process. It uses werkzeug for this, which also explictly mentions that it should not be used for production:

    The development server is not intended to be used on production systems. It was designed especially for development purposes and performs poorly under high load. For deployment setups have a look at the Application Deployment pages.

    So instead, you use a “real” WSGI server to host your application; it will then do whatever necessary to run your application properly. A WSGI server is still rather simple though; you put another web server as a reverse proxy in front to get access to the features that make webservers really powerful (e.g. load balancing, caching, SSL termination, …).