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

Tagged with:  

6 Responses to tornado.database添加PooledDB连接池功能

  1. microdict 说:

    这个方法在类中,我觉得通过继承的方式处理这个方法更稳妥些。

  2. smallfish 说:

    @microdict
    因为本身这个类就很小,直接修改之~~~

  3. 23号 说:

    from web.py:

    def get_pooled_db():
    from DBUtils import PooledDB

    # In DBUtils 0.9.3, `dbapi` argument is renamed as `creator`
    # see Bug#122112

    if PooledDB.__version__.split(‘.’) < '0.9.3'.split('.'):
    return PooledDB.PooledDB(dbapi=self.db_module, **keywords)
    else:
    return PooledDB.PooledDB(creator=self.db_module, **keywords)

  4. 23号 说:

    from web.py

    def get_pooled_db():
    from DBUtils import PooledDB

    # In DBUtils 0.9.3, `dbapi` argument is renamed as `creator`
    # see Bug#122112

    if PooledDB.__version__.split(‘.’) < '0.9.3'.split('.'):
    return PooledDB.PooledDB(dbapi=self.db_module, **keywords)
    else:
    return PooledDB.PooledDB(creator=self.db_module, **keywords)

  5. smallfish 说:

    23号 :
    from web.py
    def get_pooled_db():
    from DBUtils import PooledDB
    # In DBUtils 0.9.3, `dbapi` argument is renamed as `creator`
    # see Bug#122112
    if PooledDB.__version__.split(’.’) < ’0.9.3′.split(‘.’):
    return PooledDB.PooledDB(dbapi=self.db_module, **keywords)
    else:
    return PooledDB.PooledDB(creator=self.db_module, **keywords)

    恩,这个代码我看过,我这因为DBUtils版本已经超过0.9了,所以就没加判断。
    猜想如果玩tornado的话,肯定应该都是新版的啦

  6. 23号 :
    from web.py
    def get_pooled_db():
    from DBUtils import PooledDB
    # In DBUtils 0.9.3, `dbapi` argument is renamed as `creator`
    # see Bug#122112
    if PooledDB.__version__.split(’.’) < '0.9.3'.split('.'):
    return PooledDB.PooledDB(dbapi=self.db_module, **keywords)
    else:
    return PooledDB.PooledDB(creator=self.db_module, **keywords)

    恩,这个代码我看过,我这因为DBUtils版本已经超过0.9了,所以就没加判断。
    猜想如果玩tornado的话,肯定应该都是新版的啦

23号 发表评论 取消回复

电子邮件地址不会被公开。 必填项已被标记为 *

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

iBlog by PageLines

WP SlimStat