1. 程式人生 > >Python使用PyMysql模組報錯:lock wait timeout exceeded; try restarting transactio

Python使用PyMysql模組報錯:lock wait timeout exceeded; try restarting transactio

呵呵,我只想說:關於這個問題我整了兩個星期,關於這個問題的原因,從網上看到的很多文章全都是說要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確實比較坑啊。