亲宝软件园·资讯

展开

利用python list完成最简单的DB连接池方法

人气:0

先来看查看效果:

在代码连接数据库后,并且执行三条sql后,将mysql直接重启掉,故我们的连接池连接均是不ok的,所以,它会全部删除再抓新的连接下来,重启mysql命令:

关于python代码:

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
#!/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()

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论