Python 运行 Flask 时的警告消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50284753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:26:28  来源:igfitidea点击:

Warning message while running Flask

pythonflask

提问by Harshit Satya

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

当我从命令行运行 Flask 代码时,出现警告:

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?

这是什么意思?

回答by Arthur Dent

As stated in the Flask documentation:

正如Flask 文档中所述

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.

虽然轻量级且易于使用,但 Flask 的内置服务器不适合生产,因为它不能很好地扩展并且默认一次只处理一个请求。

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.

鉴于 Web 应用程序需要处理来自多个用户的多个并发请求,Flask 警告您开发服务器不会这样做(默认情况下)。它建议使用Web 服务器网关接口 (WSGI) 服务器部署文档中列出了许多可能性,每个可能性都有进一步的说明),它将充当您的 Web/应用程序服务器,并在服务请求时调用 Flask。

回答by lashgar

Try gevent:

尝试 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

注意:安装 gevent 使用 pip install gevent

回答by Christian Hur

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

Flask 1.x 开始,默认环境设置为生产。

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

要使用开发环境,请创建一个名为的文件.flaskenv并将其保存在项目目录的顶级(根)中。FLASK_ENV=development.flaskenv文件中设置。您还可以保存FLASK_APP=myapp.py.

Example:

例子:

myproject/.flaskenv:

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.

那应该注意警告。

回答by Franck Dernoncourt

To remove the "Do not use the development server in a production environment." warning, run:

去掉“不要在生产环境中使用开发服务器”。警告,运行:

export FLASK_ENV=development

导出 FLASK_ENV=开发

before flask run.

之前flask run

回答by Arian saputra

in configurations or config you can add this code : ENV = ""

在配置或配置中,您可以添加以下代码: ENV = ""

same as if you try to add debug set to true like this DEBUG = True

就像您尝试像这样将调试集添加为 true 一样 DEBUG = True

for more detail you can check this http://flask.pocoo.org/docs/1.0/config/#ENV

有关更多详细信息,您可以查看此http://flask.pocoo.org/docs/1.0/config/#ENV

回答by gok han

I was typing flask run and then saw this message after that I solve this issue with these:

我正在输入 flask run ,然后看到这条消息,然后我用这些解决了这个问题:

1- Add this text in your myproject/.flaskenv :
FLASK_APP=myapp.py
FLASK_ENV=development
also you should type "pip3 install python-dotenv" for using this file .flaskenv

1- 在你的 myproject/.flaskenv 中添加这个文本:
FLASK_APP=myapp.py
FLASK_ENV=development
你也应该输入“pip3 install python-dotenv”来使用这个文件 .flaskenv

2-in your project folder type in terminal your flask command which one you use :
flask-3 run

2-在您的项目文件夹中,在终端中输入您使用的烧瓶命令:
flask-3 run

回答by Parth Patel

First, try to the following :

首先,尝试以下操作:

set FLASK_ENV=development 

then run your app.

然后运行你的应用程序。

回答by user13476428

If you encounter NoAppExceptionand you see lazy loading the following seemed to fix the issue:

如果您遇到NoAppException并看到延迟加载,以下内容似乎可以解决问题:

cd <project directory>
export FLASK_APP=.
export FLASK_ENV=development
export FLASK_DEBUG=1

回答by Shashank Gupta

I have been using flask for quite some time now, and today, suddenly this warning turned up. I found this.

我已经使用烧瓶有一段时间了,今天突然出现了这个警告。我找到了这个。

As mentioned here, as of flask version 1.0 the environment in which a flask app runs is by default set to production. If you run your app in an older flask version, you won't be seeing this warning.

正如这里提到的,从flask 1.0 版开始,flask 应用程序运行的环境默认设置为生产。如果您在较旧的 Flask 版本中运行您的应用程序,您将不会看到此警告。

New in version 1.0.

Changelog

The environment in which the Flask app runs is set by the FLASK_ENV environment variable. If not set it defaults to production. The other recognized environment is development. Flask and extensions may choose to enable behaviors based on the environment.

1.0 版中的新功能。

变更日志

Flask 应用程序运行的环境由 FLASK_ENV 环境变量设置。如果未设置,则默认为生产。另一个公认的环境是发展。Flask 和扩展可以选择启用基于环境的行为。