tornado.database添加PooledDB连接池功能

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具体可以参照下:http://www.webwareforpython.org/DBUtils/Docs/UsersGuide.zh.html

你可能感兴趣的:(Python)