iro/controller/pool.py
branchdevel
changeset 132 80a334e2aae7
child 135 f8640c663e3e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/iro/controller/pool.py	Mon Feb 06 12:31:47 2012 +0100
@@ -0,0 +1,29 @@
+from twisted.python.threadpool import ThreadPool
+from twisted.internet import threads
+
+class Pool:
+    def __init__(self,name,maxthreads):
+        self.maxthreads = maxthreads
+        self.pool =  ThreadPool(minthreads=1, maxthreads=maxthreads, name=name)
+        self.reactor = None
+
+    def start(self, reactor):
+        self.pool.start()
+        self.reactor = reactor
+        self.reactor.addSystemEventTrigger('before', 'shutdown', self.pool.stop)
+
+    def run(self,f,*args,**kwargs):
+        """To run a function in Twisted's thread pool"""
+        return threads.deferToThreadPool(self.reactor, self.pool, f, *args, **kwargs)
+
+taskPool = Pool('task',5)
+dbPool = Pool('database',5)
+
+
+pools=[taskPool,dbPool]
+
+def startPool(reactor):
+    '''starting all pools'''
+    for pool in pools:
+        pool.start(reactor)
+