0

I have a Flask application that takes a big HTTP request as input (multiple MB) processes the request and sends back the response.

When I switched from Flask to Gunicorn, the performance dropped by a whole lot. The HTTP request takes much longer to reach the server.

Here is a proof of the issue:

# server.py
#
# FLASK_APP=server flask run --host=0.0.0.0 --port=3913
# gunicorn -c python:conf --bind :3913 start:server
# the developement http server of Flask receives the data much faster than the Gunicorn http server
# Run both servers and observe the difference in performance

from flask import Flask, request

application = Flask(__name__)

@application.route('/', methods=['POST'])
def length_measure():
    l = len(request.data)
    return f"<p>length of data: {l}</p>"

if __name__ == "__main__":
    import requests
    import random
    from time import time
    import numpy as np

    ip = '127.0.0.1'
    port = 3913
    Ns = [n * 10 ** 6 for n in range(10, 200, 20)]
    for N in Ns:
        data = np.random.randint(0,256, N, np.uint8).tobytes()

        t_start = time()
        requests.post(url=f'http://{ip}:{port}/', data = data)
        t_end = time()
        print(f'request for {N/ 10**6} MB took: {(t_end - t_start) * 1000} ms')
# conf.py
worker_class = "gthread"
workers = 1
threads = 1

max_requests = 0
max_requests_jitter = 0
# requirements.txt
requests==2.26.0
numpy==1.21.5
Flask==2.0.2

Performance with Flask:

request for 10.0 MB took: 12.374639511108398 ms
request for 30.0 MB took: 22.186756134033203 ms
request for 50.0 MB took: 33.45680236816406 ms
request for 70.0 MB took: 45.677900314331055 ms
request for 90.0 MB took: 57.04450607299805 ms
request for 110.0 MB took: 68.31645965576172 ms
request for 130.0 MB took: 80.84750175476074 ms
request for 150.0 MB took: 92.0867919921875 ms
request for 170.0 MB took: 103.64985466003418 ms
request for 190.0 MB took: 114.6247386932373 ms

Performance with Gunicorn:

request for 10.0 MB took: 54.90422248840332 ms
request for 30.0 MB took: 155.32922744750977 ms
request for 50.0 MB took: 264.3101215362549 ms
request for 70.0 MB took: 346.4088439941406 ms
request for 90.0 MB took: 457.01146125793457 ms
request for 110.0 MB took: 540.3439998626709 ms
request for 130.0 MB took: 634.152889251709 ms
request for 150.0 MB took: 723.6642837524414 ms
request for 170.0 MB took: 815.3290748596191 ms
request for 190.0 MB took: 916.2747859954834 ms

I have a core i7 10th gen, 32GB... running on ubuntu20.04, python3.8

I ran these tests on the same machine so obviously there is no networking issue... On seperate machines, the performance is even worse!

Note: I plan on using nginx on top of gunicorn... so any advices upon that would be highly appreciated. Thanks

davidism
  • 121,510
  • 29
  • 395
  • 339
  • Could you show the CPU and memory usage for both of the tests? – chehsunliu Dec 28 '21 at 19:27
  • 3
    Does this answer your question? https://stackoverflow.com/questions/67938278/waitress-and-gunicorn-large-data-input-is-much-slower-than-flask-development-ser – match Dec 28 '21 at 19:46

0 Answers0