利用python list 完成最簡單的DB鏈接池

先來看查看效果:python

 

 

在代碼鏈接數據庫後,而且執行三條sql後,將mysql直接重啓掉,故咱們的鏈接池鏈接均是不ok的,因此,它會所有刪除再抓新的鏈接下來,重啓mysql命令:mysql

 

 

關於python代碼:sql

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import pymysql
import random
import time

# 初始化mysql 鏈接池
def MySQLInit(mysql_host,mysql_port,mysql_user,mysql_password,mysql_charset,mysql_dbname,mysql_maxconnect):

    # 定義返回mysql鏈接池的類型,這裏定義爲 list     
    return_db_list = []

    for Connect in range(0,mysql_maxconnect):
        try:
            db = pymysql.connect(
                host = mysql_host , 
                port = mysql_port , 
                user = mysql_user , 
                passwd = mysql_password ,
                db = mysql_dbname ,
                charset = mysql_charset 
            )

            print ("鏈接MySQL數據庫成功,MySQL數據庫ID爲:%s" %(db))
            return_db_list.append(db)
        except Exception as e:
            print ("鏈接MySQL數據庫出錯")
            print (e)

    # 若是mysql鏈接池爲空,則返回-1
    if 0 == len(return_db_list):
        print ("錯誤 , 鏈接MySQL數據庫失敗,鏈接池爲0")
        return -1

    else:
        # 返回mysql鏈接池
        return return_db_list

def Pull_DB_Connect(mysql_host,mysql_port,mysql_user,mysql_password,mysql_charset,mysql_dbname , MySQL_Connect_Pool , mysql_maxconnect) :
    
    # 打印補充前的mysql鏈接數
    print ("補充前MySQL鏈接數爲:",len(MySQL_Connect_Pool))
    
    # 循環相差的鏈接數
    for Connect in range((len(MySQL_Connect_Pool)) , mysql_maxconnect):
        try:
            db = pymysql.connect(
                host = mysql_host , 
                port = mysql_port , 
                user = mysql_user , 
                passwd = mysql_password ,
                db = mysql_dbname ,
                charset = mysql_charset 
            )

            print ("從新補充,鏈接MySQL數據庫成功,MySQL數據庫ID爲:%s" %(db))
            # 將剛剛鏈接成功的DB鏈接追加到MySQL_Connect_Pool中
            MySQL_Connect_Pool.append(db)
        except Exception as e:
            print ("從新補充,鏈接MySQL數據庫出錯")
            print (e)

    if 0 == len(MySQL_Connect_Pool):
        print ("錯誤 , 鏈接MySQL數據庫失敗,鏈接池爲0")
        return -1

    else:
        # 打印鏈接池總數而且返回
        print("Mysql 鏈接池總數爲:" , len(MySQL_Connect_Pool))
        return MySQL_Connect_Pool 

def main() :

    # 定義測試mysql信息
    mysql_host = "192.168.1.100"
    mysql_port = 3306
    mysql_user = "liwang" 
    mysql_password = "liwang"
    mysql_charset = "utf8"
    mysql_dbname = "itchat"
    mysql_maxconnect = 3

    # 初始化mysql鏈接池
    MySQL_Connect_Pool = MySQLInit(mysql_host,mysql_port,mysql_user,mysql_password,mysql_charset,mysql_dbname,mysql_maxconnect)

    # 定義初始化sql 
    sql = "show databases;"

    
    while True:
        # 隨機獲鏈接池的下標
        db_link = random.randint(0,len(MySQL_Connect_Pool)-1)
        try:
            # 執行sql 
            cursor = MySQL_Connect_Pool[db_link].cursor()
            cursor.execute(sql)
            print (cursor.fetchall())
        except Exception as e:
            # 若是執行失敗,模擬爲DB鏈接失效,則打印錯誤信息,刪除錯誤的MySQL_Connect_Pool信息
            print (e)
            del MySQL_Connect_Pool[db_link]
            print ("數據庫沒法鏈接,刪除Mysql鏈接池下標:" , db_link)
            # 從新補充mysql鏈接池
            Pull_DB_Connect(mysql_host,mysql_port,mysql_user,mysql_password,mysql_charset,mysql_dbname,MySQL_Connect_Pool,mysql_maxconnect)
        
        # 休眠10s 
        time.sleep(10)

if __name__ == "__main__" :
    main()
相關文章
相關標籤/搜索