iro/model/pool.py
branchdevel
changeset 117 351a02310dd8
child 132 80a334e2aae7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/iro/model/pool.py	Fri Jan 27 21:21:41 2012 +0100
@@ -0,0 +1,25 @@
+from decorator import decorator
+from twisted.python.threadpool import ThreadPool
+from twisted.internet import threads
+
+POOL_SIZE=5     #how many threads should the db connector pool should have
+
+class Data:
+    def __init__(self):
+        self.pool =  ThreadPool(minthreads=1, maxthreads=POOL_SIZE, name='database')
+        self.reactor = None
+
+data = Data()
+#a valid dbDefer decorator
+
+def startPool(reactor): 
+    data.pool.start()
+    data.reactor = reactor
+    data.reactor.addSystemEventTrigger('before', 'shutdown', data.pool.stop)
+
+@decorator
+def runInDBPool(f,*args,**kwargs):
+    """Decorator to run DB queries in Twisted's thread pool"""
+    return threads.deferToThreadPool(data.reactor, data.pool, f, *args, **kwargs)
+
+