数据库链接池之threading.local线程版

线程标识ident

import threading
from threading import get_ident

def task():
    ident = get_ident()
    print(ident)
for i in range(20):
    t = threading.Thread(target=task)
    t.start()

数据库链接池之threading.local线程版

import pymysql
import threading
from concurrent.futures import ThreadPoolExecutor
from DBUtils.PooledDB import PooledDB


class SqlHelper(object):
    def __init__(self):
        self.pool = PooledDB(
            creator=pymysql,    # 使用链接数据库的模块
            maxconnections=6,   # 连接池允许的最大连接数,0和None表示不限制连接数
            mincached=2,        # 初始化时,链接池中至少创建的链接,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='127.0.0.1',
            port=3306,
            user='root',
            password='123456',
            database='flask',
            charset='utf8'
        )
        self.local = threading.local()

    def open(self):
        conn = self.pool.connection()
        cursor = conn.cursor(pymysql.cursors.DictCursor)
        return conn, cursor

    def close(self, conn, cursor):
        cursor.close()
        conn.close()

    def fetchall(self, sql, *args):
        conn, cursor = self.open()
        cursor.execute(sql, args)
        conn.commit()
        result = cursor.fetchall()
        self.close(conn, cursor)
        return result

    def fetchone(self, sql, *args):
        conn, cursor = self.open()
        cursor.execute(sql, args)
        conn.commit()
        result = cursor.fetchone()
        self.close(conn, cursor)
        return result

    def insert(self, sql, *args):
        conn, cursor = self.open()
        cursor.execute(sql, *args)
        conn.commit()
        result = cursor.fetchall()
        self.close(conn, cursor)
        return result

    def __enter__(self):
        conn, cursor = self.open()
        rv = getattr(self.local, 'stack', None)
        if not rv:
            self.local.stack = [(conn, cursor), ]
        else:
            rv.append((conn, cursor))
            self.local.stack = rv
        return cursor

    def __exit__(self, exc_type, exc_val, exc_tb):
        rv = getattr(self.local, 'stack', None)
        if not rv:
            # del self.local.stack
            return
        conn, cursor = self.local.stack.pop()
        cursor.close()
        conn.close()


db = SqlHelper()


def task(n):
    with db as cursor:
        cursor.execute('select 1')
    return n

多线程版

for i in range(10):
    t = threading.Thread(target=task, args=(i,))
    t.start()

线程池版

with ThreadPoolExecutor(max_workers=10) as t:
    t.submit(task, 1)

数据库链接池threading.local其他用法

import pymysql
import threading
from DBUtils.PooledDB import PooledDB

POOL = PooledDB(
            creator=pymysql,  # 使用链接数据库的模块
            maxconnections=6,  # 连接池允许的最大连接数,0和None表示不限制连接数
            mincached=2,  # 初始化时,链接池中至少创建的链接,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='127.0.0.1',
            port=3306,
            user='root',
            password='222',
            database='cmdb',
            charset='utf8'
        )

class SqlHelper(object):
    def __init__(self):
        self.conn = None
        self.cursor = None

    def open(self):
        conn = POOL.connection()
        cursor = conn.cursor()
        return conn, cursor

    def close(self):
        self.cursor.close()
        self.conn.close()

    def __enter__(self):
        self.conn,self.cursor = self.open()
        return self.cursor

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()


################### 使用 ##################

with SqlHelper() as c1:
    c1.execute('select 1')
    with SqlHelper() as c2:
        c2.execute('select 2')
    print(666)

with SqlHelper() as cursor:
    cursor.execute('select 1')

with SqlHelper() as cursor:
    cursor.execute('select 1') 
posted @ 2019-11-22 21:04  阿浪阿浪  阅读(258)  评论(0编辑  收藏  举报