How can I set a request timeout using Python Flask? I\'m trying to compare Flask to some other framework and need to configure the timeouts to be equivalent.
Thanks!
As Martijn Pieters said in their comment on the question, this isn't something you want to do because the Flask development server isn't a good choice for production. It would be better to run your flask application on a server like Gunicorn and set the timeout there instead.
But to answer the question anyway, Flask.run has an options parameter allowing you to pass options through to the underlying Werkzeug server:
run(host=None, port=None, debug=None, load_dotenv=True, **options)
The relevant werkzeug method in turn has a request_handler parameter allowing you to specify what request handler is to be used:
werkzeug.serving.run_simple(hostname,
port,
application,
use_reloader=False,
use_debugger=False,
use_evalex=True,
extra_files=None,
reloader_interval=1,
reloader_type='auto',
threaded=False,
processes=1,
request_handler=None,
static_files=None,
passthrough_errors=False,
ssl_context=None
)
This is your hook for supplying a request handler object that implements the timeout policy you want (a subject explored in this question: How to implement Timeout in BaseHTTPServer.BaseHTTPRequestHandler Python).