wordpress 多站点 子目录,网页开发入门,电影免费在线观看,企业网站建设中图片优化的方法在python下用sqlite3, 多线程 在UPDATE 或者INSERT的时候, 会报错
sqlite3.OperationalError: cannot commit - no transaction is active
1. 原因
多线程写冲突
非原子写操作#xff1a;如果多个线程同时执行非原子写操作#xff0c;可能会导致数据覆盖或不一致。
2. 解…在python下用sqlite3, 多线程 在UPDATE 或者INSERT的时候, 会报错
sqlite3.OperationalError: cannot commit - no transaction is active
1. 原因
多线程写冲突
非原子写操作如果多个线程同时执行非原子写操作可能会导致数据覆盖或不一致。
2. 解决方案
使用锁
使用锁来确保同一时间只有一个线程可以执行写操作。
import sqlite3
import threadinglock threading.Lock()def worker(conn, thread_id):with lock:cursor conn.cursor()cursor.execute(INSERT INTO test (id, value) VALUES (?, ?), (thread_id, fvalue_{thread_id}))conn.commit()cursor.close()def main():conn sqlite3.connect(example.db, check_same_threadFalse)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(targetworker, args(conn, i))threads.append(thread)thread.start()for thread in threads:thread.join()conn.close()if __name__ __main__:main()使用事务
显式事务管理在每个线程中显式地开始和提交事务确保事务的原子性和一致性。
import sqlite3
import threadingdef worker(conn, thread_id):cursor conn.cursor()cursor.execute(BEGIN)cursor.execute(INSERT INTO test (id, value) VALUES (?, ?), (thread_id, fvalue_{thread_id}))conn.commit()cursor.close()def main():conn sqlite3.connect(example.db, check_same_threadFalse)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(targetworker, args(conn, i))threads.append(thread)thread.start()for thread in threads:thread.join()conn.close()if __name__ __main__:main()