This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rute
Recipients doko, erno, ghaering, gregburd, gregory.p.smith, jcea, josiahcarlson, pitrou, rhettinger, rute, skip.montanaro
Date 2010-01-09.21:58:36
SpamBayes Score 2.6346527e-07
Marked as misclassified No
Message-id <1263074319.1.0.738748278218.issue3783@psf.upfronthosting.co.za>
In-reply-to
Content
Multi threading:

According to http://www.sqlite.org/cvstrac/wiki?p=MultiThreading
we need to keep a connection for each thread to support multi threaded
access to the database. 

I came across this when deploying an application in a multi threaded
environment. Solution is to keep the connection in the thread local-data.

Also note that a memory database is not shared between threads and
a hairy workaround with a separate working thread with queues for access
is needed. A memory database could perhaps be disallowed as the dbm is file only?

import threading

class SQLhash(collections.MutableMapping):
    def __init__(self, filename=':memory:', flags='r', mode=None):
        self.__filename = filename
        self.__local = threading.local()
        
        MAKE_SHELF = 'CREATE TABLE IF NOT EXISTS shelf (key TEXT PRIMARY KEY, value TEXT NOT NULL)'
        self.conn.execute(MAKE_SHELF)
        self.conn.commit()
    
    @property
    def conn(self):
        try:
            conn = self.__local.conn
        except AttributeError:
            conn = self.__local.conn = sqlite3.connect(self.__filename)
            self.conn.text_factory = bytes
            
        return conn
History
Date User Action Args
2010-01-09 21:58:39rutesetrecipients: + rute, skip.montanaro, rhettinger, doko, gregory.p.smith, jcea, ghaering, josiahcarlson, pitrou, erno, gregburd
2010-01-09 21:58:39rutesetmessageid: <1263074319.1.0.738748278218.issue3783@psf.upfronthosting.co.za>
2010-01-09 21:58:37rutelinkissue3783 messages
2010-01-09 21:58:36rutecreate