![[python][peewee]一个MySQL数据库死锁错误](http://pic.xiahunao.cn/yaotu/[python][peewee]一个MySQL数据库死锁错误)
死锁通常发生在多个事务同时操作相同的数据行但以不同的顺序获取锁时。在你的代码中可能同时有多个进程/线程在执行删除和插入操作。解决方案1. 添加重试机制推荐import time from peewee import OperationalError def execute_with_retry(func, max_retries3, delay0.1): 带重试机制的数据库操作 for attempt in range(max_retries): try: return func() except OperationalError as e: if e.args[0] 1213: # Deadlock error code if attempt max_retries - 1: raise time.sleep(delay * (2 ** attempt)) # 指数退避 else: raise使用方式def saveTsStockFinaIndicator(self, df): def delete_operation(): return TsStockFinaIndicator.delete().where(conditions).execute() # 使用重试机制执行删除 execute_with_retry(delete_operation) # 然后执行插入操作 # ...预防措施索引优化确保conditions中的字段有合适的索引减少事务范围只在必要的时候使用事务监控死锁使用SHOW ENGINE INNODB STATUS查看死锁详情代码审查检查是否有多个地方同时操作同一张表