diff options
author | Aurélien Bompard <aurelien@bompard.org> | 2012-09-06 11:07:52 +0200 |
---|---|---|
committer | Aurélien Bompard <aurelien@bompard.org> | 2012-09-07 10:41:52 +0200 |
commit | 65a790155e15d9671ca55377ab9fb30147ebcc0a (patch) | |
tree | a154f0f7270e9b0f8f5d95efa396d828780709ce /kittystore/storm | |
parent | 56c5f6d46a1545059c41064e877e123f9b95defa (diff) | |
download | kittystore-65a790155e15d9671ca55377ab9fb30147ebcc0a.tar.gz kittystore-65a790155e15d9671ca55377ab9fb30147ebcc0a.tar.xz kittystore-65a790155e15d9671ca55377ab9fb30147ebcc0a.zip |
Move the store pool from HyperKitty to the Storm module
Diffstat (limited to 'kittystore/storm')
-rw-r--r-- | kittystore/storm/__init__.py | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/kittystore/storm/__init__.py b/kittystore/storm/__init__.py index e4d716d..8174e55 100644 --- a/kittystore/storm/__init__.py +++ b/kittystore/storm/__init__.py @@ -3,6 +3,7 @@ from __future__ import absolute_import, with_statement import sys +import threading import storm.tracer from storm.locals import create_database, Store @@ -13,12 +14,37 @@ from . import schema from .store import StormStore +class ThreadSafeStorePool(object): + """ + Storm does not have a thread pool, like SQLAlchemy. Solve the threading + problem by keeping the store in a thread-local object. + + http://unpythonic.blogspot.fr/2007/11/using-storm-and-sqlite-in-multithreaded.html + """ + + def __init__(self, url, debug): + self.url = url + self.debug = debug + self._local = threading.local() + + def get(self): + try: + return self._local.store + except AttributeError: + self._local.store = self.create_store() + return self._local.store + + def create_store(self): + if self.debug: + storm.tracer.debug(True, stream=sys.stdout) + database = create_database(self.url) + store = Store(database) + dbtype = self.url.partition(":")[0] + dbschema = Schema(schema.CREATES[dbtype], [], [], schema) + dbschema.upgrade(store) + return StormStore(store, self.debug) + + def get_storm_store(url, debug=False): - if debug: - storm.tracer.debug(True, stream=sys.stdout) - database = create_database(url) - store = Store(database) - dbtype = url.partition(":")[0] - dbschema = Schema(schema.CREATES[dbtype], [], [], schema) - dbschema.upgrade(store) - return StormStore(store, debug) + store_pool = ThreadSafeStorePool(url, debug) + return store_pool.get() |