app.run(debug=True),会导致flask_sqlalchemy错误! #5656
-
开启debug模式后,debug=Ture的时候,flask_sqlalchemy的with self.app.app_context()会重复执行。 复现办法: model.py: userdb.py: 会出现两次input输入,之后会提示重复后程序停止。 目前只要把app.run(debug=True)改为app.run(debug=False),例如: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You're asking why code is running twice when using the debugger. Running the debugger runs the reloader. The reloader reloads at the very start to set things up. Your code executes, then is reloaded and executes again. You need to guard it with the following, similar to the from werkzeug.serving import is_running_from_reloader
if not is_running_from_reloader():
# setup code that should only run once |
Beta Was this translation helpful? Give feedback.
You're asking why code is running twice when using the debugger. Running the debugger runs the reloader. The reloader reloads at the very start to set things up. Your code executes, then is reloaded and executes again. You need to guard it with the following, similar to the
if __name__ == "__main__"
pattern.