maf*_*sis 65 python flask gunicorn
我一直在使用nginx/gunicorn和Flask为我的应用程序开发一个新的开发平台.
Ops-wise,一切正常 - 我遇到的问题是调试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
谢谢你们
Nic*_*kiy 77
接受解决方案对我不起作用.
Gunicorn是一个预分叉环境,显然Flask调试器在分叉环境中不起作用.
注意
即使交互式调试器在分叉环境中不起作用(这使得它几乎不可能在生产服务器上使用)[...]
即使你设置了app.debug = True,如果你运行,你仍然只会得到一个带有内部服务器错误消息的空白页面gunicorn testserver:app.用gunicorn做的最好的就是用它来运行它gunicorn --debug testserver:app.除了内部服务器错误消息之外,这还为您提供了跟踪.但是,这与您在终端中看到的文本跟踪相同,而不是Flask调试器.
将该if __name__ ...部分添加到testserver.py并运行python testserver.py以在开发中启动服务器将获得Flask调试器.换句话说,如果你想要Flask调试器,请不要在开发中使用gunicorn.
app = Flask(__name__)
app.config['DEBUG'] = True
if __name__ == '__main__':
    app.run()
我个人仍然喜欢使用foreman start,而不是python testserver.py因为它为我设置了所有的env变量.为了让这个工作:
Procfileweb: bin/web
bin/web文件的内容是相对于项目根目录的#!/bin/sh
if [ "$FLASK_ENV" == "development" ]; then
        python app.py
else
        gunicorn app:app -w 3
fi
.env使用以下内容创建相对于项目根目录的文件(此处为 docs )FLASK_ENV=development
DEBUG=True
另外,不要忘记将app.config['DEBUG']...生产线中的行更改为testserver.py无法在调试模式下运行Flask的内容.
app.config['DEBUG'] = os.environ.get('DEBUG', False)
maf*_*sis 46
Flask配置完全独立于gunicorn.继上配置文件的文件瓶,一个很好的解决方案将是我的源改成这样:
app = Flask(__name__)
app.config.from_pyfile('config.py')
在config.py中:
DEBUG = True
ari*_*sfl 23
对于Heroku用户,有一个比创建像Nick建议的bin/web脚本更简单的解决方案.
而不是foreman start,foreman run python app.py  如果您想在开发中调试您的应用程序,请使用它.
| 归档时间: | 
 | 
| 查看次数: | 52585 次 | 
| 最近记录: |