[英]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()
Procfile
web: bin/web
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
.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.