Created
August 2, 2013 18:00
-
-
Save ipconfiger/6141949 to your computer and use it in GitHub Desktop.
4 blog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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