Created
July 25, 2017 08:04
-
-
Save geekKeen/466b112993a5e3b37271317727867d9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import g | |
def get_db(): | |
db = getattr(g, '_database', None) | |
if db is None: | |
db = g._database = connect_to_database() | |
return db | |
@app.teardown_appcontext | |
def teardown(exception): | |
db = getattr(g, '_database', None) | |
if db is not None: | |
db.close() | |
from werkzeug.local import LocalProxy | |
db = LocalProxy(get_db) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
注意 werkzeug 的用法, 通过这种方法,可以实现类似current_app 的功能, 隐式调用
g
保存的是 app 的内容