pymysql.err.InterfaceError: (0, '')

问题描述

存储期货数据过程中,使用python的threadpool开启了多个线程按照合约进行存储,出现pymysql.err.InterfaceError:(0, '')错误

tradeback.print_exc()信息如下

问题分析

  • cursor
    查阅资料后 大部分说是问题出在 global cursor上
# 修改前伪代码
# 因为数据库按月份分表  所以让cursor每个月数据execute一次 然后等所有月份跑完close
cursor = db.cursor()
for year_month in list_year_month:
    data_insert_list = []
    sql_insert = ""
    cursor.executemany(sql_insert , data_insert_list )
cursor.close()

# 修改后伪代码
# 每个月都重新获取cursor并且close
for year_month in list_year_month:
    data_insert_list = []
    sql_insert = ""
    
    cursor = db.cursor()
    cursor.executemany(sql_insert , data_insert_list )
    cursor.close()

测试,仍然出问题,不过之前那种写法的确应该有问题,不能把cursor作为global cursor。

  • Mysql配置

继续思考,问题可能不在程序端,是不是Mysql配置问题

期货数据量比较大 可能是每次executemany超时导致 修改超时时间

修改my.ini文件

还是报错。

继续修改其他配置
connect_timeout=36000 使链接维持10小时

max_allowed_packet = 150M 使执行数据量150M

  • 代码优化
    发现出错的sql都是条数插入较多的(4000+)语句
    修改代码 分批次执行 每500条数据插入一处
    问题解决!!

总结

为了解决这个小Bug 涉及的东西还挺多 专门记录一下 下次使用pymysql一定要注意。

posted @ 2021-11-15 16:27  LazyTiming  阅读(324)  评论(0编辑  收藏  举报