【发布时间】:2014-11-12 07:47:28
【问题描述】:
我对 Flask 中的 @app.teardown_request 装饰器有疑问。
我的代码是这样的
@app.teardown_request
def teardown_request(exception):
print 'teardown'
@app.after_request
def after_request(response):
print 'after'
return response
@app.route('/entire', methods=['GET'])
def entire():
print 'entire'
return 'This is a text'
@app.route('/chunked', methods=['GET'])
def chunked():
text = 'This is a text'
def gen(t):
print 'chunked'
for a in t:
yield a
return gen(text)
当我转到 /entire 端点时,我得到了
after
teardown
entire
当我转到 /chunked 端点时,我得到了
chunked
after
teardown
因此,当以大块的方式返回数据时,请求拆卸实际上是在我返回任何数据之前发生的(也不执行任何生成此数据的代码)。
来自 sqlalchemy 会话的数据,我发现自己在对查询进行任何操作之前关闭了会话 - 然后我得到的行为是到处都是 idle in transaction...
【问题讨论】:
标签: python flask sqlalchemy flask-sqlalchemy