Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the limitations of the flask built-in web server

I'm a newbie in web server administration. I've read multiple times that flask built-in web server is not designed for "production", and must be used only for tests and debug...

But what if my app touchs only a thousand users who occasionnaly send data to the server ?

If it works, when will I have to bother with the configuration of a more sophisticated web server ? (I am looking for approximative metrics).

In a nutshell, I would love to find what the builtin web server can do (with approx thresholds) and what it cannot.

Thanks a lot !

like image 231
edelans Avatar asked Dec 30 '13 16:12

edelans


People also ask

Why is Flask built-in server not suitable for production?

While lightweight and easy to use, Flask's built-in server is not suitable for production as it doesn't scale well and by default serves only one request at a time.

Does Flask have a built-in web server?

Although Flask has a built-in web server, as we all know, it's not suitable for production and needs to be put behind a real web server able to communicate with Flask through a WSGI protocol. A common choice for that is Gunicorn—a Python WSGI HTTP server.

What is Flask web server?

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.

What is flask server?

Flask Server | How does Flask server work with Examples? Flask server is defined as server software that is capable of running HTTP requests on the public world wide web, private LAN, and private WANs and comprises of one or many computers bundled together and dedicatedly working for running the software application on the worldwide web.

Why flask is the best framework for web development?

The simplicity in the flask framework enables the developer to navigate around and create the application easily. Unlike other web application frameworks, flask let you be in total control in web development taking full creative control of the application and web development.

How do I deploy a flask application?

The recommended approach is to use a production WSGI serverto run your Flask application. There's a whole section dedicated to deployment in the docs: Deployment Options. Deploying your application is as simple as installing a WSGI server like uWSGI or gunicorn and running that instead of Flask's development server:

What are the disadvantages of using flask?

Another issue about flask is that it has a singular source which means that it will handle every request in turns, one at a time. So if you are trying to serve multiple requests, it will take more time. With fewer tools at your disposal, you may need to install more modules.


Video Answer


2 Answers

There isn't one right answer to this question, but here are some things to keep in mind:

With the right amount of horizontal scaling, it is quite possible you could keep scaling out use of the debug server forever. When exactly you would need to start scaling (or switch to using a "real" web server) would also depend on the environment you are hosting in, the expectations of the users, etc.

The main issue you would probably run into is that the server is single-threaded. This means that it will handle each request one at a time, serially. This means that if you are trying to serve more than one request (including favicons, static items like images, CSS and Javascript files, etc.) the requests will take longer. If any given requests happens to take a long time (say, 20 seconds) then your entire application is unresponsive for that time (20 seconds). This is only the default, of course: you could bump the thread counts (or have requests be handled in other processes), which might alleviate some issues. But once again, it can still be slow under a "high" load. What is considered a "high" load will be dependent on your application and the expectations of a maximum acceptable response time.

Another issue is security: if you are concerned at ALL about security (and not just the security of the data in the application itself, but the security of the box that will be running it as well) then you should not use the development server. It is not ready to withstand any sort of attack.

Finally, the development server could just fail outright. It is not designed to be used as a long-running process (days, weeks, months), and so it has not been well tested to work in this capacity.

So, yes, it has limitations. Yes, you could still conceivably use it in production. And yes, I would still recommend using a "real" web server. If you don't like the idea of needing to install something like Apache or Nginx, you can still go with a solution that is still as easy as "run a python script" by using some of the WSGI Standalone servers, which can run a server that is designed to be in production with something just as simple as running python run_app.py in the command line. You typically just need to create a 4-5 line python script to import and create the server object, point it to your Flask app, and run it.

gunicorn could be run with only the following on the command line, no extra script needed:

gunicorn myproject:app

...where "myproject" is the Python package that contains the app Flask object. Keep in mind that one of developers of gunicorn would probably recommend against this approach. See https://serverfault.com/questions/331256/why-do-i-need-nginx-and-something-like-gunicorn.

like image 128
Mark Hildreth Avatar answered Oct 11 '22 07:10

Mark Hildreth


The OP has long-since moved on, but for those who encounter this question in the future I would just add that setting up an Apache server, even on a laptop, is free and pretty easy. It can be readily configured for as few or as many features as you want just by uncomment in or commenting out lines in the config file. There might be an even easier GUI method for doing that nowdays, but just editing the configs is simple.

like image 34
Todd Carney Avatar answered Oct 11 '22 08:10

Todd Carney