打印异常导致flask应用程序在保存时停止运行

时间:2018-06-04 02:09:15

标签: python python-3.x flask wsgi

当我从终端运行我的Flask应用程序时,无论何时保存python文件,它都会死掉:

$ python3 runserver.py 
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 293-325-620
 * Detected change in '/Users/Jakob/Desktop/dev_projects/app.py', reloading
Traceback (most recent call last):
  File "runserver.py", line 9, in runserver
    app.run(host='0.0.0.0', port=port, debug=True)
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 943, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 812, in run_simple
    reloader_type)
  File "/usr/local/lib/python3.6/site-packages/werkzeug/_reloader.py", line 273, in run_with_reloader
    reloader.run()
  File "/usr/local/lib/python3.6/site-packages/werkzeug/_reloader.py", line 163, in run
    self.trigger_reload(filename)
  File "/usr/local/lib/python3.6/site-packages/werkzeug/_reloader.py", line 138, in trigger_reload
    sys.exit(3)
SystemExit: 3

Traceback (most recent call last):
  File "runserver.py", line 9, in runserver
    app.run(host='0.0.0.0', port=port, debug=True)
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 943, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 812, in run_simple
    reloader_type)
  File "/usr/local/lib/python3.6/site-packages/werkzeug/_reloader.py", line 275, in run_with_reloader
    sys.exit(reloader.restart_with_reloader())
SystemExit: 0

为什么这样做?

我可以用这个小应用重现问题:

app.py

from flask import Flask
from flask import request
from flask import render_template
app = Flask(__name__)
@app.route('/')
def index():
    return render_template('index.html')

runserver.py

import os
import traceback
from app import app


def runserver():
    port = int(os.environ.get('PORT', 5000))
    try:
      app.run(host='0.0.0.0', port=port, debug=True)
    except:
      print(traceback.format_exc())  # here's the culprit
      pass

if __name__ == '__main__':
    runserver()

我正在为一个新的开发人员编写这个项目,所以他可以看到任何出错的打印回溯。这始终是一个本地机器项目,永远不会公开。

0 个答案:

没有答案