Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support the pymsql for the gevent compatible #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#### 补丁说明
由于Gevent对MySQLDB库不支持,因此采用pymysql库,使用纯python的方式进行异步处理

####G-Firefly简介+Firefly-Gevent重要迭代版本alpha 0.1.5介绍
在alpha 0.1.5做了如下的改进:
1、单node节点断开与root节点的连接后自动重连。
Expand Down
48 changes: 40 additions & 8 deletions gfirefly/gfirefly/dbentrust/dbpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,47 @@

@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):
# """
# """
# 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):
"""
Expand All @@ -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):
"""
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion gfirefly/gfirefly/dbentrust/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion gfirefly/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"affinity",
"python-memcached",
"MySQL-python",
"gevent-zeromq"
"gevent-zeromq",
"PyMySQL"
],
entry_points="""
# -*- Entry points: -*-
Expand Down