sharing a :memory: database between different threads in python using sqlite3 package

Viewed 11139

I would like to create a :memory: database in python and access it from different threads. Essentially something like:

class T(threading.Thread):
    def run(self):
        self.conn = sqlite3.connect(':memory:')
        # do stuff with the database

for i in xrange(N):
    T().start()

and have all the connections referring to the same database.

I am aware of passing check_same_thread=True to the connect function and sharing the connection between threads but would like to avoid doing that if possible. Thanks for any help.

EDIT: corrected a typo. I originally said "have all the connections referring to the same thread" substituting thread for database.

2 Answers
Related