0
app.py的代码示例:为什么Flask-SQLAlchemy代码块执行两次?
# imports here
app = Flask(__name__)
app.config.from_pyfile('app.cfg')
db.init_app(app)
with app.app_context():
if os.path.exists('database/example.db'):
print 'already exists'
else:
print 'database created'
db.create_all()
#routes here
if __name__ == '__main__':
app.run()
在初始负载,将打印“创建的数据库”,然后“已经存在”。如果我在创建数据库时再次加载它,“已存在”会打印两次。
already exists
* Running on http://127.0.0.1:5000/
* Restarting with reloader
already exists
两个部分的问题:
1)为什么与app.app_context()块中执行两次?
2)什么是更好的方式来写这个代码?
正如输出所示,它是自动代码重新载入器,可以重新启动您的代码。它应该在应用程序运行时代码发生更改时执行此操作,它只能在调试模式下启用(也许应显示'app.cfg')。但是,在数据库已经存在的情况下,我真的不知道有什么变化,所以没有线索。 – burnpanck 2014-10-08 06:25:34