1.1 Pymysql 线程池-多线程

from threading import Thread
import pymysql
from dbutils.pooled_db import PooledDB

POOL = PooledDB(
    creator=pymysql,  # 使用链接数据库的模块
    maxconnections=20,  # 连接池允许的最大连接数,0和None表示不限制连接数
    mincached=5,  # 初始化时,链接池中至少创建的链接,0表示不创建
    blocking=True,  # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错
    ping=0,  # ping MySQL服务端,检查是否服务可用。# 如:0 = None = never, 1 = default = whenever it is requested, 2 = when a cursor is created, 4 = when a query is executed, 7 = always

    host='home.vimll.com',
    port=xxxxxx,
    user='python',
    password='xxxxxx',
    database='test',
    charset='utf8'
)

def task(num, sql):
    # 去连接池中获取一个连接
    conn = POOL.connection()
    cursor = conn.cursor()
    # cursor.execute('select * from `order`')
    # cursor.execute('select sleep(3)')
    cursor.execute(sql)
    result = cursor.fetchall()
    cursor.close()

    conn.close()   # 将连接放会到连接池
    print(num, '------------>', result)

for i in range(50):
    sql = 'select * from `order`;'
    t = Thread(target=task, args=(i, sql))
    t.start()