【pymysql】 多线程报错问题:Packet sequence number wrong - got 1 expected 2

一、场景

    使用python脚本,批量复制mock数据用于测试

 

 

二、原脚本

from concurrent.futures import ThreadPoolExecutor

from common.db.mysql_connect_client import MysqlConnectCom

client = MysqlConnectCom(DATABASE, USER, PASSWORD,HOST)

......
def task_to_run_in_parallel(data, db_client): sql = f"INSERT INTO test.student ({sql_keys}) VALUES{sql_values};" res = client.insert_operation(sql) # 创建一个线程池 with ThreadPoolExecutor(max_workers=100) as executor: # 使用map函数将任务列表中的每个元素都提交给线程池执行 future_to_tasks = {executor.submit(task_to_run_in_parallel, task, client): task for task in result} # 等待所有的任务都完成 for future in future_to_tasks: future.result() # 等待任务完成并且捕获异常
client.close_db()

 

 

三、修改后的脚本

from concurrent.futures import ThreadPoolExecutor

from common.db.mysql_connect_client import MysqlConnectCom

client = MysqlConnectCom(DATABASE, USER, PASSWORD,HOST)
......
result = client.select_operation(sql1)
......

def task_to_run_in_parallel(data, db_client):
    client1 = MysqlConnectCom(DATABASE, USER, PASSWORD,HOST)

    sql = f"INSERT INTO test.student ({sql_keys}) VALUES{sql_values};"
    res = client.insert_operation(sql)
    client1.close_db()

# 创建一个线程池
with ThreadPoolExecutor(max_workers=100) as executor:
    # 使用map函数将任务列表中的每个元素都提交给线程池执行
    future_to_tasks = {executor.submit(task_to_run_in_parallel, task, client): task for task in result}

    # 等待所有的任务都完成
    for future in future_to_tasks:
        future.result()  # 等待任务完成并且捕获异常

client.close_db()

 

参考链接:

pymysql 多线程报错问题:Packet sequence number wrong - got 1 expected 2'NoneType' object has no attribute 'read' - 飞扬92 - 博客园

posted @ 2024-08-07 11:21  代码诠释的世界  阅读(121)  评论(0编辑  收藏  举报