【问题标题】:Debugging a Flask app running in Gunicorn调试在 Gunicorn 中运行的 Flask 应用程序
【发布时间】:2012-02-15 13:34:05
【问题描述】:

我一直在为我的应用程序使用 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 中运行烧瓶应用的命令:

gunicorn -c gunicorn.conf.py testserver:app

谢谢大家

【问题讨论】:

    标签: python flask gunicorn


    【解决方案1】:

    接受的解决方案对我不起作用。

    Gunicorn 是一个预分叉环境,显然是 the Flask debugger doesn't work in a forking environment

    注意

    即使交互式调试器在 分叉环境(这使得它几乎不可能在 生产服务器)[...]

    即使您设置了app.debug = True,如果您使用gunicorn testserver:app 运行,您仍然只会得到一个带有消息Internal Server Error 的空白页面。你可以用 gunicorn 做的最好的事情就是用gunicorn --debug testserver:app 运行它。除了 Internal Server Error 消息之外,这还为您提供了跟踪信息。但是,这只是您在终端中看到的相同文本跟踪,而不是 Flask 调试器。

    if __name__ ... 部分添加到 testserver.py 并运行 python testserver.py 以启动开发中的服务器,即可获得 Flask 调试器。 换句话说,如果你想要 Flask 调试器,就不要在开发中使用 gunicorn。

    app = Flask(__name__)
    app.config['DEBUG'] = True
    
    if __name__ == '__main__':
        app.run()
    

    ## Heroku 用户提示: 就我个人而言,我仍然喜欢使用`foreman start`,而不是`python testserver.py`,因为[它为我设置了所有环境变量](https://devcenter.heroku.com/articles/config-vars#using-领班)。要让它工作:

    Procfile的内容

    web: bin/web
    

    bin/web的内容,文件相对于项目根目录

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

    在开发中,创建一个相对于项目根目录的.env 文件,内容如下(文档here

    FLASK_ENV=development
    DEBUG=True
    

    另外,不要忘记将 testserver.py 中的 app.config['DEBUG']... 行更改为不会在生产中以调试模式运行 Flask 的内容。

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

    【讨论】:

    • 请注意,最初的问题实际上是关于让堆栈跟踪完全显示。我不使用交互式调试器。不错的答案。
    • 这些文档中的 .env 文件是针对 heroku envs 的。如果您设置 heroku env FLASK_ENV=development,那么当它实时运行 procfile 时,它​​将执行第一个 if 并运行 python app.py。调试仍设置为 True。或者我错过了什么。
    • 值得注意的是,您必须使用 chmod +x bin/web 使 bin/web 可执行。
    • @nick-zalutskiy --debug flag has been removed since version 19.3,你能更新你的答案吗?
    【解决方案2】:

    Flask 配置与 gunicorn 完全不同。在the Flask documentation on config files 之后,一个好的解决方案是将我的源代码更改为:

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

    在 config.py 中:

    DEBUG = True
    

    【讨论】:

    • 如果我们将 gunicorn 和 Flask 视为离散的实体,这当然是有道理的。不像在 gunicorn 中运行 Django。
    • 这是正确的答案。默认情况下,Flask 在生产模式下运行,不会报告错误。要在生产模式下处理错误,您可以查看以下内容:flask.pocoo.org/docs/errorhandling
    • 哪个版本? gunicorn:错误:无法识别的参数:-d gunicorn:错误:无法识别的参数:--debug $ gunicorn -v gunicorn(版本 19.9.0)
    【解决方案3】:

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

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

    【讨论】:

    • 问题是如果你有多个进程它不会启动例如web: gunicorn server:app worker: python worker.py
    • +1 这会在浏览器中提供标准调试输出,而不仅仅是在终端中。
    【解决方案4】:

    我在 gunicorn 下运行烧瓶时遇到了类似的问题,我没有在浏览器中看到堆栈跟踪(每次都必须查看日志)。设置 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)。

    【讨论】:

      【解决方案5】:

      我用过这个:

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

      【讨论】:

        【解决方案6】:

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

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

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

        【讨论】:

        • 该标志只显示 gunicorn 级别的调试,而不是实际的应用程序代码错误。
        猜你喜欢
        • 2022-07-07
        • 2022-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-18
        • 1970-01-01
        • 2021-05-22
        相关资源
        最近更新 更多