Harrison Harrison - 3 months ago 176
Python Question

Flask threaded = True?

What exactly does putting

threaded = True
in my
app.run()
do?

My application processes input from the user, and takes a bit of time to do so. During this time, the application is unable to handle other requests.

I'm looking at options that allow me to handle more than 1 request at a time with Flask. I read that the basic Flask server component is really only meant for testing during development. I have tested my application with
threaded = True
and it's now allowing me to handle multiple requests concurrently.

I have a few questions:


  1. How many requests will my application be able to handle concurrently with this statement?

  2. What are the downsides to using this? If i'm not expecting more than a few requests concurrently, can I just continue to use this? My application is going to be used within my office, so I don't expect to have more than 2 or 3 requests at a time.


Answer

Normally, the WSGI server included with Flask is run in single-threaded mode, and can only handle one request at a time. Any parallel requests will have to wait until they can be handled, which can lead to issues if you tried to contact your own server from a request.

With threaded=True requests are each handled in a new thread. How many threads your server can handle concurrently depends entirely on your OS and what limits it sets on the number of threads per process. The implementation uses the SocketServer.ThreadingMixIn class, which sets no limits to the number of threads it can spin up.

Note that the Flask server is designed for development only. It is not a production-ready server. Don't rely on it to run your site on the wider web. Use a proper WSGI server like gunicorn or uWSGI) instead.

Comments