I know I can link Flask with Apache or other web servers. But, I was thinking of running Flask as a standalone server serving multiple clients simultaneously.
Is this possible? Do I have to handle spawning multiple threads and managing them?
I know I can link Flask with Apache or other web servers. But, I was thinking of running Flask as a standalone server serving multiple clients simultaneously.
Is this possible? Do I have to handle spawning multiple threads and managing them?
flask.Flask.run
accepts additional keyword arguments (**options
) that it forwards towerkzeug.serving.run_simple
- two of those arguments arethreaded
(which you can set toTrue
to enable threading) andprocesses
(which you can set to a number greater than one to have werkzeug spawn more than one process to handle requests). So if you do:Flask will tell Werkzeug to use threading and to spawn three processes to handle incoming requests.
That being said, Werkzeug's
serving.run_simple
wraps the standard library'swsgiref
package - and that package contains a reference implementation of WSGI, not a production-ready web server. If you are going to use Flask in production (assuming that "production" is not a low-traffic internal application with no more than 10 concurrent users) make sure to stand it up behind a real web server (see the section of Flask's docs entitled Deployment Options for some suggested methods).Using the simple
app.run()
from within Flask creates a single synchronous server on a single thread capable of serving only one client at a time. It is intended for use in controlled environments with low demand (i.e. development, debugging) for exactly this reason.Spawning threads and managing them yourself is probably not going to get you very far either, because of the Python GIL.
That said, you do still have some good options. Gunicorn is a solid, easy-to-use WSGI server that will let you spawn multiple workers (separate processes, so no GIL worries), and even comes with asynchronous workers that will speed up your app (and make it more secure) with little to no work on your part (especially with Flask).
Still, even Gunicorn should probably not be directly publicly exposed. In production, it should be used behind a more robust HTTP server; nginx tends to go well with Gunicorn and Flask.