Flask and Gunicorn

· by

Contents

Flask is a Python web framework and Gunicorn is a Python Web Server Gateway Interface (WSGI) HTTP server.

Setup

Install the requirements:

$ pip3 install flask gunicorn

Create an exampleproject.py:

import random
from flask import Flask

app = Flask(__name__)
random.seed(0)


@app.route("/")
def hello():
    x = random.randint(1, 100)
    y = random.randint(1, 100)
    return str(x * y)


if __name__ == "__main__":
    app.run(host="0.0.0.0")

and a wsgi.py:

from exampleproject import app

if __name__ == "__main__":
    app.run()

Run it with:

$ gunicorn --bind 0.0.0.0:5000 wsgi:app

Benchmarking it

I want to see when this fails. So I test it with ApacheBench (ab):

$ sudo apt-get install apache2-utils

And then I run 50 000 requests with different concurrency levels.

Seen Errors

Connection reset by peer (104)

I got a lot of apr_socket_recv: Connection reset by peer (104) with gunicorn.

See also