1 from twisted.python.threadpool import ThreadPool |
1 from twisted.python.threadpool import ThreadPool |
2 from twisted.internet import threads |
2 from twisted.internet import threads |
3 |
3 |
4 class Pool: |
4 class Pool: |
|
5 """wrapper class to handles a twisted threadpool""" |
5 def __init__(self,name,maxthreads): |
6 def __init__(self,name,maxthreads): |
|
7 """ |
|
8 :param string name: name of the threadpool |
|
9 :param integer maxthreads: how many thread a allowed at maximum |
|
10 """ |
6 self.maxthreads = maxthreads |
11 self.maxthreads = maxthreads |
7 self.pool = ThreadPool(minthreads=1, maxthreads=maxthreads, name=name) |
12 self.pool = ThreadPool(minthreads=1, maxthreads=maxthreads, name=name) |
8 self.reactor = None |
13 self.reactor = None |
9 |
14 |
10 def start(self, reactor): |
15 def start(self, reactor): |
|
16 """stats the pool and adds the pool.stop function to the stop procedure. |
|
17 |
|
18 :param reactor: a valid twisted reactor |
|
19 """ |
11 self.pool.start() |
20 self.pool.start() |
12 self.reactor = reactor |
21 self.reactor = reactor |
13 self.reactor.addSystemEventTrigger('before', 'shutdown', self.pool.stop) |
22 self.reactor.addSystemEventTrigger('before', 'shutdown', self.pool.stop) |
14 |
23 |
15 def run(self,f,*args,**kwargs): |
24 def run(self,f,*args,**kwargs): |
16 """To run a function in Twisted's thread pool""" |
25 """run a function in Twisted's thread pool""" |
17 return threads.deferToThreadPool(self.reactor, self.pool, f, *args, **kwargs) |
26 return threads.deferToThreadPool(self.reactor, self.pool, f, *args, **kwargs) |
18 |
27 |
19 #task Pool for sending |
|
20 taskPool = Pool('task',5) |
28 taskPool = Pool('task',5) |
|
29 """taskpool to handle sending data""" |
21 |
30 |
22 #db Pool to handle reqests to sqlalchemy |
|
23 dbPool = Pool('database',5) |
31 dbPool = Pool('database',5) |
|
32 """pool to handle database connection via sqlalchemy""" |
24 |
33 |
25 # all pools in one list |
|
26 pools=[taskPool,dbPool] |
34 pools=[taskPool,dbPool] |
|
35 """all available pools""" |
27 |
36 |
28 def startPool(reactor): |
37 def startPool(reactor): |
29 '''starting all pools''' |
38 '''run start function for all items in :attr:`pools`''' |
30 for pool in pools: |
39 for pool in pools: |
31 pool.start(reactor) |
40 pool.start(reactor) |
|
41 |
|
42 __all__=["Pool", "startPool", "dbPool", "taskPool", "pools"] |