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!
It would be better to run your flask application on a server like Gunicorn and set the timeout there instead. 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.
Timeouts can be easily added to the URL you are requesting. It so happens that, you are using a third-party URL and waiting for a response. It is always a good practice to give a timeout on the URL, as we might want the URL to respond within a timespan with a response or an error.
The HyperText Transfer Protocol (HTTP) 408 Request Timeout response status code means that the server would like to shut down this unused connection. It is sent on an idle connection by some servers, even without any previous request by the client.
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).
WSGI is the protocol, a facade separating your code from the actual web server which runs it.
In the flask, you only create the logic of the server. On run, your Flask application is served by the WSGI server. The most common is uWSGI ( & nGinx proxy to the secure border between the outer world and your server). But you can use whichever WSGI server suits you best without need to change your code (nichol.as/benchmark-of-python-web-servers)*.
Flask itself ships only with the Development WSGI flask server. It means the server uses a lot of features to help the developer debug their application. These features make the server very slow. So when you do a benchmark of your app on the Development WSGI flask server, results have no value for you.
On the other hand, specialized production-ready WSGI servers (including uWSGI) are well optimized, tested and production-proven. Most of the features are usually turned off by default and allow you to fine-tune these powerful beasts for your application.
Deployment tutorials:
Now when I explained the context, back to your original question. I would set requests timeout in your:
import requests requests.get('https://api.myapp.com', timeout=3) <Response [200]>
http { server { … location / { … proxy_read_timeout 120s; … } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With