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
myyou
V2EX  ›  Python

python mysqldb 使用完需要关闭吗?

  •  
  •   myyou · 2016-04-11 18:53:23 +08:00 · 4876 次点击
    这是一个创建于 2964 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在 python 中新建连接 con=MySQLdb.connect("127.0.0.1","root","password","database")获取游标 cursor=con.cursor() 在 con.commit()结束后需要关闭运行 con.close()还是只需要运行 cursor.close()关闭游标不关闭连接?

    9 条回复    2016-04-12 10:12:44 +08:00
    peter999
        1
    peter999  
       2016-04-11 19:02:02 +08:00   ❤️ 1
    cursor.close()
    con.close()
    icedx
        2
    icedx  
       2016-04-11 19:03:48 +08:00   ❤️ 1
    单线程环境不建议频繁 con.close()
    多线程请交给 ORM 框架来做
    myyou
        3
    myyou  
    OP
       2016-04-11 19:04:24 +08:00
    @peter999 con.close()关闭后游标不是会自动关闭吗?
    myyou
        4
    myyou  
    OP
       2016-04-11 19:06:10 +08:00
    @icedx 单线程环境是不是只需要关闭游标啊?
    peter999
        5
    peter999  
       2016-04-11 19:15:42 +08:00   ❤️ 1
    教程上是这么写的,我一般用连接池,很少直接用链接了
    icedx
        6
    icedx  
       2016-04-11 19:43:37 +08:00   ❤️ 1
    @myyou 是的
    myyou
        7
    myyou  
    OP
       2016-04-12 10:05:31 +08:00
    @peter999 你说的连接池是 PooledDB 吗?这个连接池需要关闭吗?
    peter999
        8
    peter999  
       2016-04-12 10:09:41 +08:00   ❤️ 1
    需要,不过不是关闭数据库,只是释放回连接池。如果使用 django 这种自带 orm 的,就不需要 close 了
    myyou
        9
    myyou  
    OP
       2016-04-12 10:12:44 +08:00
    @peter999 明白了谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1844 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 16:35 · PVG 00:35 · LAX 09:35 · JFK 12:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.