diff --git a/README.md b/README.md index 19fc7ac..2a305ae 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ +#### 补丁说明 +由于Gevent对MySQLDB库不支持,因此采用pymysql库,使用纯python的方式进行异步处理 + ####G-Firefly简介+Firefly-Gevent重要迭代版本alpha 0.1.5介绍 在alpha 0.1.5做了如下的改进: 1、单node节点断开与root节点的连接后自动重连。 diff --git a/gfirefly/gfirefly/dbentrust/dbpool.py b/gfirefly/gfirefly/dbentrust/dbpool.py index 87ce01c..d5ccb11 100755 --- a/gfirefly/gfirefly/dbentrust/dbpool.py +++ b/gfirefly/gfirefly/dbentrust/dbpool.py @@ -4,9 +4,10 @@ @author: lan (www.9miao.com) ''' -from DBUtils.PooledDB import PooledDB +from DBUtils.PooledDB import PooledDB, InvalidConnection +import pymysql -DBCS = {'MySQLdb':"MySQLdb",} +# DBCS = {'MySQLdb':"MySQLdb",} # class DBPool(PooledDB): # """ @@ -14,6 +15,36 @@ # def __init__(self, creator, *args, **kwargs): # PooledDB.__init__(self, creator, *args, **kwargs) # self.config = kwargs + + +class PyMysqlProxyDBConnection: + """Proxy the pymysql connection api for the MySQLdb api""" + + def __init__(self, dbConnection): + self._dbConnection = dbConnection + + def __getattr__(self, name): + """Proxy all members of the class.""" + if self._dbConnection: + return getattr(self._dbConnection, name) + else: + raise InvalidConnection + + def close(self): + if self._dbConnection: + self._dbConnection.close() + + def cursor(self, cursorclass=None): + if cursorclass is None: + self._dbConnection.cursor() + return self._dbConnection.cursor(cursor=cursorclass) + + def __del__(self): + """Delete the pooled connection.""" + try: + self._dbConnection.close() + except Exception: + pass class MultiDBPool(object): """ @@ -28,9 +59,10 @@ def initPool(self,config): """ self.dbpool = {} for dbkey,dbconfig in config.items(): - _creator = DBCS.get(dbconfig.get('engine','MySQLdb')) - creator = __import__(_creator) - self.dbpool[dbkey] = PooledDB(creator,**dbconfig) + # _creator = DBCS.get(dbconfig.get('engine','MySQLdb')) + # creator = __import__(_creator) + # self.dbpool[dbkey] = PooledDB(creator=creator,**dbconfig) + self.dbpool[dbkey] = PooledDB(creator=pymysql, **dbconfig) def bind_router(self,router): """ @@ -53,13 +85,13 @@ def connection(self,write=True,**kw): """ """ if not self.router: - return self.dbpool.values()[0].connection(shareable=kw.get("shareable",True)) + return PyMysqlProxyDBConnection(self.dbpool.values()[0].connection(shareable=kw.get("shareable",True))) if write: dbkey = self.router.db_for_write(**kw) - return self.dbpool[dbkey].connection(shareable=kw.get("shareable",True)) + return PyMysqlProxyDBConnection(self.dbpool[dbkey].connection(shareable=kw.get("shareable",True))) else: dbkey = self.router.db_for_read(**kw) - return self.dbpool[dbkey].connection(shareable=kw.get("shareable",True)) + return PyMysqlProxyDBConnection(self.dbpool[dbkey].connection(shareable=kw.get("shareable",True))) dbpool = MultiDBPool() diff --git a/gfirefly/gfirefly/dbentrust/util.py b/gfirefly/gfirefly/dbentrust/util.py index 2fcc974..45450b7 100755 --- a/gfirefly/gfirefly/dbentrust/util.py +++ b/gfirefly/gfirefly/dbentrust/util.py @@ -6,7 +6,7 @@ ''' from dbpool import dbpool -from MySQLdb.cursors import DictCursor +from pymysql.cursors import DictCursor from numbers import Number from gtwisted.utils import log import traceback diff --git a/gfirefly/setup.py b/gfirefly/setup.py index e968636..c1e037a 100644 --- a/gfirefly/setup.py +++ b/gfirefly/setup.py @@ -28,7 +28,8 @@ "affinity", "python-memcached", "MySQL-python", - "gevent-zeromq" + "gevent-zeromq", + "PyMySQL" ], entry_points=""" # -*- Entry points: -*-