V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
loading
V2EX  ›  Python

db.close()

  •  
  •   loading ·
    ycf · 2012-04-02 22:23:33 +08:00 · 3614 次点击
    这是一个创建于 4443 天前的主题,其中的信息可能已经有所发展或是发生改变。
    在看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时随处可见。
    2 条回复    1970-01-01 08:00:00 +08:00
    kingwkb
        1
    kingwkb  
       2012-04-06 12:51:45 +08:00 via Android
    这个有没有都行,一个请求执行完毕后会释放所有资源
    loading
        2
    loading  
    OP
       2012-04-06 15:26:14 +08:00 via Android
    @kingwkb 感谢解惑
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1799 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 00:57 · PVG 08:57 · LAX 17:57 · JFK 20:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.