Python3多线程写数据库

import pymysql
import threading

class Db:
    def __init__(self, host=None, username=None, pwd=None, dbname=None):
        self.pool = {}
        self.host = host
        self.username = username
        self.pwd = pwd
        self.dbname = dbname

    def get_instance(self):
    	# 在连接池中为每个线程创建连接,并从连接池中获取连接
        name = threading.current_thread().name
        if name not in self.pool:
            conn = pymysql.connect(self.host, self.username, self.pwd, self.dbname)
            self.pool[name] = conn
        return self.pool[name]

class MultiThreads:
    def __init__(self, host=None, username=None, pwd=None, dbname=None):
        self.max_id = 10000
        self.start_id = 1
        self.db = Db(host, username, pwd, dbname)
        self.lock = threading.Lock()

    def insert_data(self):
        db = self.db.get_instance()
        cursor = db.cursor()
        while True:
            if self.start_id >= self.max_id:
                break
            s = self.start_id
            # 线程锁
            with self.lock:
                self.start_id += 50
                if self.start_id > self.max_id:
                    self.start_id = self.max_id
            e = self.start_id
            for i in range(s, e):
                sql = "insert into xxxxxx (id, name) values('{}', '{}')".format(i, threading.current_thread().name)
                try:
                    cursor.execute(sql)
                    db.commit()
                    print(threading.current_thread().name, ': ', sql, ': success')
                except:
                    db.rollback()
                    print(threading.current_thread().name, ': ', sql, ':failed')
                    raise

def main():
    multi_threads = MultiThreads('xxxxxxx', 'xxxxx', 'xxxxx', 'xxxxxx')
    threads = []
    # 150为线程数量
    for i in range(150):
        t = threading.Thread(target=multi_threads.insert_data)
        t.start()
        threads.append(t)
    for t in threads:
        t.join()

if __name__ == '__main__':
    main()

该示例代码运行需要mysql数据库,修改其中关于数据库连接参数以及表的参数即可运行
结果:
Python3多线程写数据库_第1张图片

你可能感兴趣的:(工具,Python,数据处理)