iro/model/pool.py
branchdevel
changeset 132 80a334e2aae7
parent 117 351a02310dd8
child 151 ff8448fb07e7
equal deleted inserted replaced
131:c51c3e8c3ec0 132:80a334e2aae7
     1 from decorator import decorator
     1 from decorator import decorator
     2 from twisted.python.threadpool import ThreadPool
       
     3 from twisted.internet import threads
       
     4 
       
     5 POOL_SIZE=5     #how many threads should the db connector pool should have
       
     6 
     2 
     7 class Data:
     3 class Data:
     8     def __init__(self):
     4     def __init__(self):
     9         self.pool =  ThreadPool(minthreads=1, maxthreads=POOL_SIZE, name='database')
     5         self.pool =  None
    10         self.reactor = None
       
    11 
     6 
    12 data = Data()
     7 data = Data()
    13 #a valid dbDefer decorator
     8 #a valid dbDefer decorator
    14 
     9 
    15 def startPool(reactor): 
    10 def setPool(pool): 
    16     data.pool.start()
    11     data.pool = pool
    17     data.reactor = reactor
       
    18     data.reactor.addSystemEventTrigger('before', 'shutdown', data.pool.stop)
       
    19 
    12 
    20 @decorator
    13 @decorator
    21 def runInDBPool(f,*args,**kwargs):
    14 def runInDBPool(f,*args,**kwargs):
    22     """Decorator to run DB queries in Twisted's thread pool"""
    15     """Decorator to run DB queries in Twisted's thread pool"""
    23     return threads.deferToThreadPool(data.reactor, data.pool, f, *args, **kwargs)
    16     return data.pool.run(f, *args, **kwargs)
    24 
    17 
    25 
    18