zoukankan      html  css  js  c++  java
  • [转载]tornado.database添加PooledDB连接池功能

    转载自 http://chenxiaoyu.org/2009/12/01/python-tornado-dbutil.html

    tornado.database模块简单包装了下对MySQL的操作,短小精悍。

    无奈源码中无连接池功能,遂加上了一段DBUtils模块功能。

    主要修改了reconnect()方法,大致在database.py第86行左右。(tornado 0.2 win版)

    原代码如下:

    def reconnect(self):
            """Closes the existing database connection and re-opens it."""
            self.close()
            self._db = MySQLdb.connect(**self._db_args)
            self._db.autocommit(True)

    修改后:

    def reconnect(self):
            """Closes the existing database connection and re-opens it."""
            self.close()
            try:
                from DBUtils import PooledDB
                pool_con = PooledDB.PooledDB(creator=MySQLdb, **self._db_args)
                self._db = pool_con.connection()
            except:
                self._db = MySQLdb.connect(**self._db_args)
                self._db.autocommit(True)

    至于安装DBUtils模块可以去http://pypi.python.org/pypi/DBUtils/下载,也可以简单的用easy_install:

    easy_install -U DBUtils

    PooledDB有这么几个参数:

    * creator
        可以生成 DB-API 2 连接的任何函数或 DB-API 2 兼容的数据库连接模块。
    * mincached
        启动时开启的空连接数量(缺省值 0 意味着开始时不创建连接)
    * maxcached
        连接池使用的最多连接数量(缺省值 0 代表不限制连接池大小)
    * maxshared
        最大允许的共享连接数量(缺省值 0 代表所有连接都是专用的)
    * maxconnections
        最大允许连接数量(缺省值 0 代表不限制)
    * blocking
        设置在达到最大数量时的行为(缺省值 0 False)
    * maxusage
        单个连接的最大允许复用次数(缺省值 0 False 代表不限制的复用)
    * setsession:
        一个可选的SQL命令列表用于准备每个会话,如 ["set datestyle to german", ...]

    creator 函数或可以生成连接的函数可以接受这里传入的其他参数,例如主机名、数据库、用户名、密码等。你还可以选择传入creator函数的其他参数,允许失败重连和负载均衡。

    具体可以参照下:http://www.webwareforpython.org/DBUtils/Docs/UsersGuide.zh.html


  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    SpringCloud Alibaba微服务实战一
    Bar:柱状图/条形图
    Pie:饼图
    Gauge:仪表盘
    Funnel:漏斗图
    VSCode+latex引用bibtex参考文献
    因为报表做得太好,我被阎王爷叫走了.....
    ubuntu安装pyCUDA
  • 原文地址:https://www.cnblogs.com/ymy124/p/4991431.html
Copyright © 2011-2022 走看看