【问题标题】:How can I debug a Flask App using gunicorn如何使用 gunicorn 调试 Flask 应用程序
【发布时间】:2020-08-09 06:47:57
【问题描述】:

您好,当我访问我的网站时,我收到This site can’t be reached

 gunicorn -w 3 flaskodesiapp:create_app 
[2020-08-09 02:35:45 -0400] [35946] [INFO] Starting gunicorn 20.0.4
[2020-08-09 02:35:45 -0400] [35946] [INFO] Listening at: http://127.0.0.1:8000 (35946)
[2020-08-09 02:35:45 -0400] [35946] [INFO] Using worker: sync
[2020-08-09 02:35:45 -0400] [35948] [INFO] Booting worker with pid: 35948
[2020-08-09 02:35:45 -0400] [35949] [INFO] Booting worker with pid: 35949
[2020-08-09 02:35:45 -0400] [35950] [INFO] Booting worker with pid: 35950

但执行python3 run.py 运行代码没有问题。因此,我能够看到该应用程序。 运行.py

from flaskodesiapp import create_app

app = create_app()

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5090, debug=True)

wsig.py

from flaskodesiapp import create_app
from .config import Config

if __name__ == "__main__":
    app = create_app(config=Config)
    app.run(host="0.0.0.0", port=5090, debug=True)

create_app 来自 init.py

from flask import Flask
from flaskodesiapp.config import Config

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(Config)

    from flaskodesiapp.odesi.routes import odesi
    from flaskodesiapp.errors.handlers import errors

    app.register_blueprint(odesi)
    app.register_blueprint(errors)

    return app

配置仅包含 smtp 凭据。

【问题讨论】:

    标签: python-3.x flask gunicorn


    【解决方案1】:

    试试下面的代码

    def create_app(config_class=Config):
        app = Flask(__name__)
        app.config['DEBUG'] = True
        ...
    

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

    将 if name ... 部分添加到 testserver.py 并运行 python testserver.py 以启动开发中的服务器,即可获得 Flask 调试器。

    因此,建议您不要在开发环境中使用 gunicorn,它不会以正确的方式帮助您。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-15
      • 2022-07-07
      • 1970-01-01
      • 2018-02-22
      • 2022-01-15
      • 2013-06-22
      相关资源
      最近更新 更多