MySQL

做最专业的DBA

Python MySQL 连接池

Config.py

DB_HOST_1="192.167.121.151";
DB_HOST_2="192.167.121.152";
DB_HOST_3="192.167.121.153";
DB_HOST_4="192.167.121.154";
DB_HOST_5="192.167.121.155";
DB_HOST_6="192.167.121.156";
DB_HOST_7="192.167.121.157";
DB_HOST_8="192.167.121.158";
DB_PORT=3306;
DB_NAME="ys_api";
DB_USER="root";
DB_PWD="pwdtest";
DB_CHAR="utf8mb4";
DB_MIN_CACHED=200;
DB_MAX_CACHED=200;
DB_MAX_SHARED=3000;
DB_MAX_CONNECYIONS=10000;
DB_BLOCKING=True;
DB_MAX_USAGE=0;
DB_SET_SESSION=None;

MySQLConn.py

import MySQLdb
from MySQLdb.cursors import DictCursor
from DBUtils.PooledDB import PooledDB

import Config
#print 'class'
class MySQLConn(object):
        __pool = None
        def __init__(self):
                #for i in range(0):
                self._conn_151=MySQLConn.__getConn(self,Config.DB_HOST_1)
                self._conn_152=MySQLConn.__getConn(self,Config.DB_HOST_2)
                self._conn_153=MySQLConn.__getConn(self,Config.DB_HOST_3)
                self._conn_154=MySQLConn.__getConn(self,Config.DB_HOST_4)
                self._conn_155=MySQLConn.__getConn(self,Config.DB_HOST_5)
                self._conn_156=MySQLConn.__getConn(self,Config.DB_HOST_6)
                self._conn_157=MySQLConn.__getConn(self,Config.DB_HOST_7)
                self._conn_158=MySQLConn.__getConn(self,Config.DB_HOST_8)
                self._conn_base=MySQLConn.__getConn(self,Config.DB_HOST_8)
                #ip='192.167.121.15'+str(i+1)
                #self._conn = MySQLConn.__getConn(ip)
                #self._cursor = self._conn.cursor()



        @staticmethod
        def __getConn(self,ip):
                if MySQLConn.__pool is None:
                        #print ip,Config.DB_PORT,Config.DB_USER,Config.DB_PWD,Config.DB_CHAR
                        #__pool = PooledDB(creator=MySQLdb,mincached=1,maxcached=30,maxusage=Config.DB_MAX_USAGE,host=ip,port=Config.DB_PORT,user=Config.DB_USER,passwd=Config.DB_PWD,db=Config.DB_NAME,use_unicode=False,charset=Config.DB_CHAR,cursorclass=DictCursor,maxusage=10000) 
                        __pool = PooledDB(creator=MySQLdb,mincached=1,maxcached=10,maxusage=Config.DB_MAX_USAGE,host=ip,port=Config.DB_PORT,user=Config.DB_USER,passwd=Config.DB_PWD,db=Config.DB_NAME,use_unicode=False,charset=Config.DB_CHAR,cursorclass=DictCursor)
                return __pool.connection()

        desc='this is mysql pool !'
        def dispose(self,isEnd=1):
                if isEnd==1:
                    self.end('commit')
                else:
                    self.end('rollback');
                self._cursor.close()
                self._conn.close()
阅读更多

扫码向博主提问

去开通我的Chat快问

mchdba

博客专家

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • MySQL Hbas
  • Hbase
  • Tidb
  • cdb
  • 数据拆分
个人分类: - - - Python
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭