iro/model/pool.py
branchdevel
changeset 117 351a02310dd8
child 132 80a334e2aae7
equal deleted inserted replaced
116:48c70425bf6c 117:351a02310dd8
       
     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 
       
     7 class Data:
       
     8     def __init__(self):
       
     9         self.pool =  ThreadPool(minthreads=1, maxthreads=POOL_SIZE, name='database')
       
    10         self.reactor = None
       
    11 
       
    12 data = Data()
       
    13 #a valid dbDefer decorator
       
    14 
       
    15 def startPool(reactor): 
       
    16     data.pool.start()
       
    17     data.reactor = reactor
       
    18     data.reactor.addSystemEventTrigger('before', 'shutdown', data.pool.stop)
       
    19 
       
    20 @decorator
       
    21 def runInDBPool(f,*args,**kwargs):
       
    22     """Decorator to run DB queries in Twisted's thread pool"""
       
    23     return threads.deferToThreadPool(data.reactor, data.pool, f, *args, **kwargs)
       
    24 
       
    25