db.close()

2012-04-02 22:23:33 +08:00
 loading
在看peewee文档的例子说明时,文档中有这段:
Because sqlite likes to have a separate connection per-thread, we will tell flask that during the request/response cycle we need to create a connection to the database. Flask provides some handy decorators to make this a snap:

@app.before_request
def before_request():
g.db = database
g.db.connect()

@app.after_request
def after_request(response):
g.db.close()
return response

是不是只有用sqlite的时候才需要 db.connect(),然后 db.close呢?
我看其他例子都没见到db.close影子,记得asp+access时随处可见。
3626 次点击
所在节点    Python
2 条回复
kingwkb
2012-04-06 12:51:45 +08:00
这个有没有都行,一个请求执行完毕后会释放所有资源
loading
2012-04-06 15:26:14 +08:00
@kingwkb 感谢解惑

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/31209

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX