iro/controller/pool.py
changeset 302 3f4bdea2abbf
parent 294 0e75bd39767d
equal deleted inserted replaced
90:eb04ac3a8327 302:3f4bdea2abbf
       
     1 # Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net>
       
     2 # 
       
     3 # This file is part of Iro.
       
     4 # 
       
     5 # Permission is hereby granted, free of charge, to any person obtaining a copy of
       
     6 # this software and associated documentation files (the "Software"), to deal in
       
     7 # the Software without restriction, including without limitation the rights to use,
       
     8 # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
       
     9 # #Software, and to permit persons to whom the Software is furnished to do so,
       
    10 # subject to the following conditions:
       
    11 # 
       
    12 # The above copyright notice and this permission notice shall be included in
       
    13 # all copies or substantial portions of the Software.
       
    14 # 
       
    15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
       
    16 # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
       
    17 # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
       
    18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
       
    19 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
       
    20 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
       
    21 
       
    22 from twisted.python.threadpool import ThreadPool
       
    23 from twisted.internet import threads
       
    24 
       
    25 class Pool:
       
    26     """wrapper class to handles a twisted threadpool"""
       
    27     def __init__(self,name,maxthreads):
       
    28         """
       
    29         :param string name: name of the threadpool
       
    30         :param integer maxthreads: how many thread a allowed at maximum
       
    31         """
       
    32         self.maxthreads = maxthreads
       
    33         self.pool =  ThreadPool(minthreads=1, maxthreads=maxthreads, name=name)
       
    34         self.reactor = None
       
    35 
       
    36     def start(self, reactor):
       
    37         """stats the pool and adds the pool.stop function to the stop procedure.
       
    38         
       
    39         :param reactor: a valid twisted reactor
       
    40         """
       
    41         self.pool.start()
       
    42         self.reactor = reactor
       
    43         self.reactor.addSystemEventTrigger('before', 'shutdown', self.pool.stop)
       
    44 
       
    45     def run(self,f,*args,**kwargs):
       
    46         """run a function in Twisted's thread pool"""
       
    47         return threads.deferToThreadPool(self.reactor, self.pool, f, *args, **kwargs)
       
    48 
       
    49 taskPool = Pool('task',5)
       
    50 """taskpool to handle sending data"""
       
    51 
       
    52 dbPool = Pool('database',5)
       
    53 """pool to handle database connection via sqlalchemy""" 
       
    54 
       
    55 pools=[taskPool,dbPool]
       
    56 """all available pools"""
       
    57 
       
    58 def startPool(reactor):
       
    59     '''run start function for all items in :attr:`pools`'''
       
    60     for pool in pools:
       
    61         pool.start(reactor)
       
    62 
       
    63 __all__=["Pool", "startPool", "dbPool", "taskPool", "pools"]