<p>接受溶液对我不起作用。</p>
<p>Gunicorn是一个预分叉环境,显然是<a href="http://flask.pocoo.org/docs/quickstart/#debug-mode">the Flask debugger doesn't work in a forking environment</a>。</p>
<blockquote>
<h3>Attention</h3>
<p>Even though the interactive debugger does not work in
forking environments (which makes it nearly impossible to use on
production servers) [...]</p>
</blockquote>
<p>即使您设置了<code>app.debug = True</code>,如果您使用<code>gunicorn testserver:app</code>运行,您仍然只能得到一个带有消息<em>Internal Server Error</em>的空页面。使用gunicorn的最佳方法是使用<code>gunicorn --debug testserver:app</code>运行它。除了<em>内部服务器错误</em>消息之外,还提供了跟踪。但是,这只是您在终端中看到的相同的文本跟踪,而不是Flask调试器。</p>
<p>将<code>if __name__ ...</code>部分添加到testserver.py并运行<code>python testserver.py</code>以启动正在开发的服务器,这将为您提供Flask调试器。<strong>换句话说,如果需要Flask调试器,不要在开发中使用gunicorn。</strong></p>
<pre><code>app = Flask(__name__)
app.config['DEBUG'] = True
if __name__ == '__main__':
app.run()
</code></pre>
<p><br/></p>
<h2>Heroku用户的提示:</h2>
<p>就我个人而言,我仍然喜欢使用<code>foreman start</code>,而不是<code>python testserver.py</code>,因为<a href="https://devcenter.heroku.com/articles/config-vars#using-foreman">it sets up all the env variables for me</a>。要让它工作:</p>
<h3><code>Procfile</code></h3>的含量
<pre><code>web: bin/web
</code></pre>
<h3><code>bin/web</code>的内容,文件相对于项目根</h3>
<pre><code>#!/bin/sh
if [ "$FLASK_ENV" == "development" ]; then
python app.py
else
gunicorn app:app -w 3
fi
</code></pre>
<h3>在开发过程中,创建一个与项目根目录相关的<code>.env</code>文件,其内容如下(docs<a href="https://devcenter.heroku.com/articles/config-vars#using-foreman">here</a>)</h3>
<pre><code>FLASK_ENV=development
DEBUG=True
</code></pre>
<p>另外,不要忘记将<code>testserver.py</code>中的<code>app.config['DEBUG']...</code>行更改为在生产中不在调试模式下运行Flask的行。</p>
<pre><code>app.config['DEBUG'] = os.environ.get('DEBUG', False)
</code></pre>