• Martin Thoma
  • Home
  • Categories
  • Tags
  • Archives
  • Support me

Flask and Gunicorn

Contents

  • Setup
  • Benchmarking it
  • Seen Errors
    • Connection reset by peer (104)
  • See also

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 an 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 run 50 000 requests with different concurrency.

Seen Errors

Connection reset by peer (104)

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

See also

  • digitalocean.com: How To Serve Flask Applications with Gunicorn and Nginx on Ubuntu 16.04

Published

Jul 11, 2018
by Martin Thoma

Category

Code

Tags

  • Flask 6
  • gunicorn 2

Contact

  • Martin Thoma - A blog about Code, the Web and Cyberculture
  • E-mail subscription
  • RSS-Feed
  • Privacy/Datenschutzerklärung
  • Impressum
  • Powered by Pelican. Theme: Elegant by Talha Mansoor