Fix Python – Can I serve multiple clients using just Flask as standalone?


Asked By – ATOzTOA

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?

Now we will see solution for issue: Can I serve multiple clients using just Flask as standalone?

Answer accepts additional keyword arguments (**options) that it forwards to werkzeug.serving.run_simple – two of those arguments are threaded (a boolean) and processes (which you can set to a number greater than one to have werkzeug spawn more than one process to handle requests).

threaded defaults to True as of Flask 1.0, so for the latest versions of Flask, the default development server will be able to serve multiple clients simultaneously by default. For older versions of Flask, you can explicitly pass threaded=True to enable this behaviour.

For example, you can do

if __name__ == '__main__':

to handle multiple clients using threads in a way compatible with old Flask versions, or

if __name__ == '__main__':, processes=3)

to tell Werkzeug to spawn three processes to handle incoming requests, or just

if __name__ == '__main__':

to handle multiple clients using threads if you know that you will be using Flask 1.0 or later.

That being said, Werkzeug’s serving.run_simple wraps the standard library’s wsgiref 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).

This question is answered By – Sean Vieira

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0