First of all, note that sqlite is thread safe.
$ python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite
>>> sqlite.threadsafety
1
Then just make sure that you open a new database descriptor in each thread.
I do this using a local thread store to cache the database descriptor, so there is only one for each thread. Something like this ... (from the py2.5 program - I hope it will work with 3.0!)
import threading
class YourClass:
def __init__(self):
self.local = threading.local()
self.db_file = "/path/to/db"
def db_open(self):
if not getattr(self.local, "db", None):
self.local.db = sqlite3.connect(self.db_file)
return self.local.db
source
share