【python下用sqlite3, 多线程下报错,原因和解决 】

在python下用sqlite3, 多线程 在UPDATE 或者INSERT的时候, 会报错

sqlite3.OperationalError: cannot commit - no transaction is active

1. 原因

多线程写冲突

  • 非原子写操作:如果多个线程同时执行非原子写操作,可能会导致数据覆盖或不一致。

2. 解决方案

使用锁

使用锁来确保同一时间只有一个线程可以执行写操作。

go 复制代码
import sqlite3
import threading

lock = threading.Lock()

def worker(conn, thread_id):
    with lock:
        cursor = conn.cursor()
        cursor.execute("INSERT INTO test (id, value) VALUES (?, ?)", (thread_id, f"value_{thread_id}"))
        conn.commit()
        cursor.close()

def main():
    conn = sqlite3.connect('example.db', check_same_thread=False)
    cursor = conn.cursor()
    cursor.execute("CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, value TEXT)")
    conn.commit()
    cursor.close()

    threads = []
    for i in range(5):
        thread = threading.Thread(target=worker, args=(conn, i))
        threads.append(thread)
        thread.start()

    for thread in threads:
        thread.join()

    conn.close()

if __name__ == "__main__":
    main()

使用事务

显式事务管理:在每个线程中显式地开始和提交事务,确保事务的原子性和一致性。

go 复制代码
import sqlite3
import threading

def worker(conn, thread_id):
    cursor = conn.cursor()
    cursor.execute("BEGIN")
    cursor.execute("INSERT INTO test (id, value) VALUES (?, ?)", (thread_id, f"value_{thread_id}"))
    conn.commit()
    cursor.close()

def main():
    conn = sqlite3.connect('example.db', check_same_thread=False)
    cursor = conn.cursor()
    cursor.execute("CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, value TEXT)")
    conn.commit()
    cursor.close()

    threads = []
    for i in range(5):
        thread = threading.Thread(target=worker, args=(conn, i))
        threads.append(thread)
        thread.start()

    for thread in threads:
        thread.join()

    conn.close()

if __name__ == "__main__":
    main()
相关推荐
爬山算法13 小时前
Redis(162)如何使用Redis实现消息队列?
数据库·redis·缓存
u***324313 小时前
【Redis】centos7 systemctl 启动 Redis 失败
数据库·redis·缓存
煎蛋学姐13 小时前
SSM社区生鲜电商平台dq96z(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·用户管理·ssm 框架·社区生鲜电商·商品信息管理
杨云龙UP13 小时前
从0搭建Oracle ODA NFS异地备份:从YUM源到RMAN定时任务的全流程
linux·运维·数据库·oracle
倔强的石头_13 小时前
从 Oracle 到 KingbaseES:破解迁移痛点,解锁信创时代数据库新可能
数据库
踢足球092913 小时前
Redis的典型应用
数据库·redis·缓存
hadage23313 小时前
--- redis 常见问题 ---
数据库·redis·mybatis
O***P57113 小时前
redis批量删除namespace下的数据
数据库·redis·缓存
5***262214 小时前
SQL Server导出和导入可选的数据库表和数据,以sql脚本形式
数据库·sql
ID_1800790547314 小时前
基于 Python 的 Cdiscount 商品详情 API 调用与 JSON 核心字段解析(含多规格 SKU 提取)
开发语言·python·json