繁体   English   中英

调试在 Gunicorn 中运行的 Flask 应用程序

[英]Debugging a Flask app running in Gunicorn

我一直在为我的应用程序使用 nginx/gunicorn 和 Flask 开发一个新的开发平台。

在操作方面,一切正常 - 我遇到的问题是调试 Flask 层。 当我的代码中出现错误时,我只会直接向浏览器返回 500 错误,并且控制台或日志中没有任何显示。

我尝试了许多不同的配置/选项。我想我一定错过了一些明显的东西。

我的 gunicorn.conf:

 import os bind = '127.0.0.1:8002' workers = 3 backlog = 2048 worker_class = "sync" debug = True proc_name = 'gunicorn.proc' pidfile = '/tmp/gunicorn.pid' logfile = '/var/log/gunicorn/debug.log' loglevel = 'debug'

borks-testserver.py 的一些 Flask 代码示例:

 from flask import Flask from flask import render_template_string from werkzeug.contrib.fixers import ProxyFix app = Flask(__name__) @app.route('/') def index(): n = 1/0 return "DIV/0 worked "

最后,在 gunicorn 中运行 flask 应用程序的命令:

 gunicorn -c gunicorn.conf.py testserver:app

谢谢大家

The accepted solution doesn't work for me.

Gunicorn is a pre-forking environment and apparently the Flask debugger doesn't work in a forking environment.

Attention

Even though the interactive debugger does not work in forking environments (which makes it nearly impossible to use on production servers) [...]

Even if you set app.debug = True, you will still only get an empty page with the message Internal Server Error if you run with gunicorn testserver:app. The best you can do with gunicorn is to run it with gunicorn --debug testserver:app. That gives you the trace in addition to the Internal Server Error message. However, this is just the same text trace that you see in the terminal and not the Flask debugger.

Adding the if __name__ ... section to the testserver.py and running python testserver.py to start the server in development gets you the Flask debugger. In other words, don't use gunicorn in development if you want the Flask debugger.

app = Flask(__name__)
app.config['DEBUG'] = True

if __name__ == '__main__':
    app.run()

## Tip for Heroku users: Personally I still like to use `foreman start`, instead of `python testserver.py` since [it sets up all the env variables for me](https://devcenter.heroku.com/articles/config-vars#using-foreman). To get this to work:

Contents of Procfile

web: bin/web

Contents of bin/web, file is relative to project root

#!/bin/sh

if [ "$FLASK_ENV" == "development" ]; then
        python app.py
else
        gunicorn app:app -w 3
fi

In development, create a .env file relative to project root with the following contents (docs here)

FLASK_ENV=development
DEBUG=True

Also, don't forget to change the app.config['DEBUG']... line in testserver.py to something that won't run Flask in debug mode in production.

app.config['DEBUG'] = os.environ.get('DEBUG', False)

Flask 配置与 gunicorn 的配置完全不同。 按照Flask 配置文件文档,一个好的解决方案是将我的源代码更改为:

 app = Flask(__name__) app.config.from_pyfile('config.py')

在 config.py 中:

 DEBUG = True

对于 Heroku 用户,有一个比 Nick 建议的创建 bin/web 脚本更简单的解决方案。

如果您想在开发中调试应用程序,只需使用foreman run python app.py而不是foreman start

我在 gunicorn 下运行 flask 时遇到了类似的问题,我没有在浏览器中看到堆栈跟踪(每次都必须查看日志)。 设置 DEBUG、FLASK_DEBUG 或此页面上提到的任何内容都不起作用。 最后我这样做了:

 app = Flask(__name__) app.config.from_object(settings_map[environment]) if environment == 'development': from werkzeug.debug import DebuggedApplication app_runtime = DebuggedApplication(app, evalex=False) else: app_runtime = app

注意 evalex 被禁用,因为交互式调试不适用于分叉(gunicorn)。

我用这个:

 gunicorn "swagger_server.__main__:app" -w 4 -b 0.0.0.0:8080

您不能真正使用 gunicorn 运行它,例如在代码更改时使用 flask reload选项。

我在 api 发射点中使用了以下片段:

 app = Flask(__name__) try: if os.environ["yourapp_environment"] == "local": run_as_local = True # some other local configs eg paths app.logger.info('Running server in local development mode:') except KeyError as err. if "yourapp_environment" in err:args. run_as_local = False # some other production configs eg paths app.logger:info('No "yourapp_environment env" given so app running server in production mode.') else. raise.......:: if __name__ == '__main__'. if run_as_local. app.run(host='127.0,0,1': port='8058'. debug=True) else. app.run(host='0.0 0 0')

对于上述解决方案,您需要在控制台中提供export yourapp_environment = "local"

现在我可以运行我的本地python api.py和 prod gunicorn --bind 0.0.0.0:8058 api:app

else语句app.run()实际上并不需要,但我保留它是为了提醒我有关主机、端口等的信息。

尝试像这样在运行命令上设置调试标志

gunicorn -c gunicorn.conf.py --debug testserver:app

并在您的 Flask 应用程序中保留DEBUG = True 没有从配置文件中应用您的调试选项一定是有原因的,但现在上面的说明应该可以帮助您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM