Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning message while running Flask

Tags:

python

flask

While I am running Flask code from my command line, a warning is appearing:

Serving Flask app "hello_flask" (lazy loading)
* Environment: production
  WARNING: Do not use the development server in a production environment.
  Use a production WSGI server instead.

What does this mean?

like image 631
Harshit Satya Avatar asked May 11 '18 03:05

Harshit Satya


People also ask

How do I get alert messages on flask?

Flask provides the flash() method, in the same way, the client-side scripting language like JavaScript uses the alerts or the python GUI framework Tkinter uses the dialogue box or the message box. The flash() method is used to generate informative messages in the flask.

How do you handle a flask error?

Step 1 — Using The Flask Debugger. In this step, you'll create an application that has a few errors and run it without debug mode to see how the application responds. Then you'll run it with debug mode on and use the debugger to troubleshoot application errors.

What happens when flask run?

The flask run command can do more than just start the development server. By enabling debug mode, the server will automatically reload if code changes, and will show an interactive debugger in the browser if an error occurs during a request. The debugger allows executing arbitrary Python code from the browser.


3 Answers

As stated in the Flask documentation:

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.

Given that a web application is expected to handle multiple concurrent requests from multiple users, Flask is warning you that the development server will not do this (by default). It recommends using a Web Server Gateway Interface (WSGI) server (numerous possibilities are listed in the deployment docs with further instructions for each) that will function as your web/application server and call Flask as it serves requests.

like image 64
Arthur Dent Avatar answered Oct 20 '22 18:10

Arthur Dent


Try gevent:

from flask import Flask
from gevent.pywsgi import WSGIServer

app = Flask(__name__)

@app.route('/api', methods=['GET'])
def index():
    return "Hello, World!"

if __name__ == '__main__':
    # Debug/Development
    # app.run(debug=True, host="0.0.0.0", port="5000")
    # Production
    http_server = WSGIServer(('', 5000), app)
    http_server.serve_forever()

Note: Install gevent using pip install gevent

like image 44
lashgar Avatar answered Oct 20 '22 19:10

lashgar


As of Flask 1.x, the default environment is set to production.

To use the development environment, create a file called .flaskenv and save it in the top-level (root) of your project directory. Set the FLASK_ENV=development in the .flaskenv file. You can also save the FLASK_APP=myapp.py.

Example:

myproject/.flaskenv:

FLASK_APP=myapp.py
FLASK_ENV=development

Then you just execute this on the command line:

flask run

That should take care of the warning.

like image 19
Christian Hur Avatar answered Oct 20 '22 18:10

Christian Hur