Python使用PyMysql模块报错:lock wait timeout exceeded; try restarting transactio

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a2011480169/article/details/80135472

呵呵,我只想说:关于这个问题我整了两个星期,关于这个问题的原因,从网上看到的很多文章全都是说要conn.commit(),

但是我在程序里面已经commit()了,最后定位到的问题是Pymysql在多线程(或多进程下)面会有bug,对,你没听错,

Pymysql模块自身的bug造成的:

解决方案:利用DBUtils.PooledDB import PooledDB中的这个类来解决。

核心如下:

POOL = PooledDB(
    creator=pymysql,
    maxconnections=6,
    mincached=2,
    maxcached=5,
    maxshared=3,
    blocking=True,
    maxusage=None,
    setsession=[],
    ping=0,
    host=db_info['host'],
    port=db_info['port'],
    user=db_info['user'],
    password=db_info['password'],
    database=db_info['database'],
    charset='utf8'
)
哎,这个bug确实比较坑啊。

猜你喜欢

转载自blog.csdn.net/a2011480169/article/details/80135472