Skip to content

Instantly share code, notes, and snippets.

@ipconfiger
Created August 2, 2013 18:00
Show Gist options
  • Save ipconfiger/6141949 to your computer and use it in GitHub Desktop.
Save ipconfiger/6141949 to your computer and use it in GitHub Desktop.
4 blog
proxies = {}
class _DbWrapper():
def __init__(self,module):
self.connection=None #这个就是维护的长连接对象
self.db=module #这个是被包装的原生MySQLdb的module
def __getattr__(self, key):
return getattr(self.db, key) #代理所有不关心的函数
def connect(self,*argv,**kwargv):
“”“
替换原有的connection对象
”“”
if not self.connection:
self.connection=self.db.connect(*argv,**kwargv)
return _ConnectionWrapper(self.connection)
def manage(module,keepalive=7*3600):
“”“
返回代替原生MySQLdb模块的对象
”“”
try:
return proxies[module]
except KeyError:
return proxies.setdefault(module,_DbWrapper(module))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment