我正在尝试创建一个内存中的sqlite3缓存来存储oauth令牌,但是遇到了有关多线程的问题。在运行了几次测试之后,我注意到其行为与非内存数据库和多线程有很大的不同。
值得注意的是,如果写入器线程未提交就写入,则读取器线程将立即失败并显示“表已锁定”。即使使用isolation_level=None
,对于多个线程也是如此。
这不仅仅是在交易完成之前阻止读者,而是不管timeout
还是PRAGMA busy_timeout = 10000
,读者都会立即失败。
使它起作用的唯一方法是设置isolation_level=None
并执行PRAGMA read_uncommitted=TRUE
。但是,我宁愿不这样做。
是否可以让读取器线程简单地等待锁而不是立即失败?
import sqlite3 import threading def get_conn(name, is_memory=False, timeout=5, isolation_level='IMMEDIATE', pragmas=None): uri = 'file:%s' % name if is_memory: uri = uri + '?mode=memory&cache=shared' conn = sqlite3.connect(uri, uri=True, timeout=timeout, isolation_level=isolation_level) if pragmas is None: pragmas = [] if not isinstance(pragmas, list): pragmas = [pragmas] for pragma in pragmas: conn.execute(pragma) return conn def work1(name, is_memory=False, timeout=5, isolation_level='IMMEDIATE', pragmas=None, loops=1): conn = get_conn(name, is_memory=is_memory, timeout=timeout, isolation_level=isolation_level, pragmas=pragmas) for i in range(loops): conn.execute('INSERT INTO foo VALUES (1)') def work2(name, is_memory=False, timeout=5, isolation_level='IMMEDIATE', pragmas=None, loops=1): conn = get_conn(name, is_memory=is_memory, timeout=timeout, isolation_level=isolation_level, pragmas=pragmas) for i in range(loops): len(conn.execute('SELECT * FROM foo').fetchall()) def main(name, is_memory=False, timeout=5, isolation_level='IMMEDIATE', pragmas=None, loops=1, num_threads=16): conn = get_conn(name, is_memory=is_memory, timeout=timeout, isolation_level=isolation_level, pragmas=pragmas) try: conn.execute('CREATE TABLE foo(a int)') except sqlite3.OperationalError: conn.execute('DROP TABLE foo') conn.execute('CREATE TABLE foo(a int)') threads = [] for i in range(num_threads): threads.append(threading.Thread(target=work1, args=(name, is_memory, timeout, isolation_level, pragmas, loops))) threads.append(threading.Thread(target=work2, args=(name, is_memory, timeout, isolation_level, pragmas, loops))) for thread in threads: thread.start() for thread in threads: thread.join() # In-Memory Tests # All of these fail immediately with table is locked. There is no delay; timeout/busy_timeout has no effect. main('a', is_memory=True, timeout=5, isolation_level='IMMEDIATE', pragmas=None) main('b', is_memory=True, timeout=5, isolation_level='DEFERRED', pragmas=None) main('c', is_memory=True, timeout=5, isolation_level='EXCLUSIVE', pragmas=None) main('d', is_memory=True, timeout=5, isolation_level=None, pragmas=None) main('e', is_memory=True, timeout=5, isolation_level='IMMEDIATE', pragmas=['PRAGMA busy_timeout = 10000']) main('f', is_memory=True, timeout=5, isolation_level='DEFERRED', pragmas=['PRAGMA busy_timeout = 10000']) main('g', is_memory=True, timeout=5, isolation_level='EXCLUSIVE', pragmas=['PRAGMA busy_timeout = 10000']) main('h', is_memory=True, timeout=5, isolation_level=None, pragmas=['PRAGMA busy_timeout = 10000']) main('i', is_memory=True, timeout=5, isolation_level='IMMEDIATE', pragmas=['PRAGMA read_uncommitted=TRUE']) main('j', is_memory=True, timeout=5, isolation_level='DEFERRED', pragmas=['PRAGMA read_uncommitted=TRUE']) main('k', is_memory=True, timeout=5, isolation_level='EXCLUSIVE', pragmas=['PRAGMA read_uncommitted=TRUE']) # This is the only successful operation, when isolation_level = None and PRAGMA read_uncommitted=TRUE main('l', is_memory=True, timeout=5, isolation_level=None, pragmas=['PRAGMA read_uncommitted=TRUE']) # These start to take a really long time main('m', is_memory=True, timeout=5, isolation_level=None, pragmas=['PRAGMA read_uncommitted=TRUE'], loops=100) main('n', is_memory=True, timeout=5, isolation_level=None, pragmas=['PRAGMA read_uncommitted=TRUE'], loops=100, num_threads=128) # None of the on disk DB's ever fail: main('o', is_memory=False, timeout=5, isolation_level='IMMEDIATE', pragmas=None) main('p', is_memory=False, timeout=5, isolation_level='DEFERRED', pragmas=None) main('q', is_memory=False, timeout=5, isolation_level='EXCLUSIVE', pragmas=None) main('r', is_memory=False, timeout=5, isolation_level=None, pragmas=None) main('s', is_memory=False, timeout=5, isolation_level='IMMEDIATE', pragmas=['PRAGMA busy_timeout = 10000']) main('t', is_memory=False, timeout=5, isolation_level='DEFERRED', pragmas=['PRAGMA busy_timeout = 10000']) main('u', is_memory=False, timeout=5, isolation_level='EXCLUSIVE', pragmas=['PRAGMA busy_timeout = 10000']) main('v', is_memory=False, timeout=5, isolation_level=None, pragmas=['PRAGMA busy_timeout = 10000']) main('w', is_memory=False, timeout=5, isolation_level='IMMEDIATE', pragmas=['PRAGMA read_uncommitted=TRUE']) main('x', is_memory=False, timeout=5, isolation_level='DEFERRED', pragmas=['PRAGMA read_uncommitted=TRUE']) main('y', is_memory=False, timeout=5, isolation_level='EXCLUSIVE', pragmas=['PRAGMA read_uncommitted=TRUE']) main('z', is_memory=False, timeout=5, isolation_level=None, pragmas=['PRAGMA read_uncommitted=TRUE']) # These actually fail with database is locked main('aa', is_memory=False, timeout=5, isolation_level=None, pragmas=['PRAGMA read_uncommitted=TRUE'], loops=100) main('ab', is_memory=False, timeout=5, isolation_level=None, pragmas=['PRAGMA read_uncommitted=TRUE'], loops=100, num_threads=128)
答案 0 :(得分:0)
我不认为SQLite3接口是可重入的。我认为每个线程都必须获取一个互斥锁,执行查询,然后释放该互斥锁。尝试一次仅执行一个数据库操作。 (一般不会需要Python的API层,因为通常不需要任何东西。)