[pysqlite] PySqlite threading concurrency issue
[email protected] Fri, 12 Sep 2008 16:08:04 -0500
| Newsgroups | gmane.comp.python.db.pysqlite.user |
|---|---|
| Message-ID | <[email protected]> |
Hello all,
I've ran accross a problem when I have multiple threads in python
attempting to select/update the same database. I get the dreaded
"database is locked" error. The following bit of code reproduces the
problem fairly reliably (within about 45 seconds on my machine), and I
was wondering if anyone had any insight into what might be going on?
from pysqlite2 import dbapi2 as sqlite3
import datetime
import thread, time, os
db = 'test.db'
def aa():
while True:
conn = sqlite3.connect(db, timeout=10)
conn.isolation_level = None
c = conn.cursor()
c.execute('SELECT a from t1 where b=1')
print list(c)
#conn.commit()
conn.close()
time.sleep(10)
def bb():
while True:
conn = sqlite3.connect(db, timeout=10)
conn.isolation_level = 'EXCLUSIVE'
c = conn.cursor()
c.execute('UPDATE t1 set a=? where b=0', (datetime.datetime.now(),) )
#conn.commit()
conn.close()
time.sleep(.1)
try:
os.unlink('test.db')
except OSError:
pass
with sqliteutil.cursor(db, isolation_level = 'EXCLUSIVE', timeout=60) as c:
c.execute('create table t1(a real, b real)')
c.executemany('insert into t1 values(?, ?)', ( (0, 0) for i in
xrange(10000)) )
c.execute('insert into t1 values(0, 1)')
thread.start_new_thread(bb, ())
aa()
When I run it, I get the following:
$ python test_lock.py
[(0.0,)]
Traceback (most recent call last):
File "test_lock.py", line 59, in <module>
aa()
File "test_lock.py", line 14, in aa
c.execute('SELECT a from t1 where b=1')
pysqlite2.dbapi2.OperationalError: database is locked
Any help would be much appreciated.
Thanks.