Home > OS >  Flask server timeouts at 30 sec with gunicorn
Flask server timeouts at 30 sec with gunicorn

Time:12-03

Here is the minimal example of the code. curl request curl http://127.0.0.1:5000/get_zip/my_zip.zip -o my_zip.zip should send user the file archive/my_zip.zip

It works correctly without gunicorn and disconnects after 30 seconds, when the server is launched with gunicorn.

from os import path
from flask import Flask, request, jsonify, json, send_file

app = Flask(__name__)

@app.route('/get_zip/<file_path>', methods=['GET'])
def get_zip(file_path):  # file_path: path to a large zip file
    return send_file(path.join('archive', file_path), as_attachment=True)

if __name__ == '__main__':  
    app.run(host="0.0.0.0", port="5000", debug=False, use_reloader=False)

What is the correct way to fix this disconnect when ran under gunicorn?

CodePudding user response:

30 seconds is default timeout value for gunicorn.

To increase it use --timeout <seconds> parameter on your gunicorn config.

Also if you run gunicorn under nginx, don't forget to manage nginx's settings:

proxy_connect_timeout <seconds>s;
proxy_read_timeout <seconds>s;

UPDATE:

it's better and safer to send files from flask by using send_from_directory

  • Related