[英]Debugging a Flask app running in Gunicorn
I've been working on a new dev platform using nginx/gunicorn and Flask for my application.我一直在为我的应用程序使用 nginx/gunicorn 和 Flask 开发一个新的开发平台。
Ops-wise, everything works fine - the issue I'm having is with debugging the Flask layer.在操作方面,一切正常 - 我遇到的问题是调试 Flask 层。 When there's an error in my code, I just get a straight 500 error returned to the browser and nothing shows up on the console or in my logs.
当我的代码中出现错误时,我只会直接向浏览器返回 500 错误,并且控制台或日志中没有任何显示。
I've tried many different configs/options.. I guess I must be missing something obvious.我尝试了许多不同的配置/选项。我想我一定错过了一些明显的东西。
My gunicorn.conf:我的 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'
An example of some Flask code that borks- testserver.py: 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 "
And finally, the command to run the flask app in gunicorn:最后,在 gunicorn 中运行 flask 应用程序的命令:
gunicorn -c gunicorn.conf.py testserver:app
Thanks y'all谢谢大家
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)
The Flask config is entirely separate from gunicorn's. Flask 配置与 gunicorn 的配置完全不同。 Following the Flask documentation on config files , a good solution would be change my source to this:
按照Flask 配置文件文档,一个好的解决方案是将我的源代码更改为:
app = Flask(__name__) app.config.from_pyfile('config.py')
And in config.py:在 config.py 中:
DEBUG = True
For Heroku users , there is a simpler solution than creating a bin/web script like suggested by Nick.对于 Heroku 用户,有一个比 Nick 建议的创建 bin/web 脚本更简单的解决方案。
Instead of foreman start
, just use foreman run python app.py
if you want to debug your application in development.如果您想在开发中调试应用程序,只需使用
foreman run python app.py
而不是foreman start
。
I had similiar problem when running flask under gunicorn I didn't see stacktraces in browser (had to look at logs every time).我在 gunicorn 下运行 flask 时遇到了类似的问题,我没有在浏览器中看到堆栈跟踪(每次都必须查看日志)。 Setting DEBUG, FLASK_DEBUG, or anything mentioned on this page didn't work.
设置 DEBUG、FLASK_DEBUG 或此页面上提到的任何内容都不起作用。 Finally I did this:
最后我这样做了:
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
Note evalex is disabled because interactive debbugging won't work with forking (gunicorn).注意 evalex 被禁用,因为交互式调试不适用于分叉(gunicorn)。
I used this:我用这个:
gunicorn "swagger_server.__main__:app" -w 4 -b 0.0.0.0:8080
You cannot really run it with gunicorn and for example use the flask reload
option upon code changes.您不能真正使用 gunicorn 运行它,例如在代码更改时使用 flask
reload
选项。
I've used following snippets in my api launchpoint:我在 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')
For above solution you need to give export yourapp_environment = "local"
in the console.对于上述解决方案,您需要在控制台中提供
export yourapp_environment = "local"
。
now I can run my local as python api.py
and prod gunicorn --bind 0.0.0.0:8058 api:app
现在我可以运行我的本地
python api.py
和 prod gunicorn --bind 0.0.0.0:8058 api:app
The else
statement app.run()
is not actually needed, but I keep it for reminding me about host, port etc. else
语句app.run()
实际上并不需要,但我保留它是为了提醒我有关主机、端口等的信息。
Try setting the debug flag on the run command like so尝试像这样在运行命令上设置调试标志
gunicorn -c gunicorn.conf.py --debug testserver:app
and keep the DEBUG = True
in your Flask application.并在您的 Flask 应用程序中保留
DEBUG = True
。 There must be a reason why your debug option is not being applied from the config file but for now the above note should get you going.没有从配置文件中应用您的调试选项一定是有原因的,但现在上面的说明应该可以帮助您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.