
I have been looking for a way to run different processes in a single reactor without having to share the threadpool class and this is what i have come up with. Twisted fans, lemi know if you have another way of doing it. much of the code have been pulled from the reactor base.py from twisted.internet import reactor,threadsfrom time import sleep class Thread: def __init__(self,name="threadpool1"): self.name=name self.threadpool = None def _initThreadPool(self): """ Create the threadpool accessible with callFromThread. """ from twisted.python import threadpool self.threadpool = threadpool.ThreadPool( 0, 10, self.name) self._threadpoolStartupID = reactor.callWhenRunning( self.threadpool.start) self.threadpoolShutdownID = reactor.addSystemEventTrigger( 'during', 'shutdown', self._stopThreadPool) def _stopThreadPool(self): """ Stop the reactor threadpool. This method is only valid if there is currently a threadpool (created by L{_initThreadPool}). It is not intended to be called directly; instead, it will be called by a shutdown trigger created in L{_initThreadPool}. """ triggers = [self._threadpoolStartupID, self.threadpoolShutdownID] for trigger in filter(None, triggers): try: reactor.removeSystemEventTrigger(trigger) except ValueError: pass self._threadpoolStartupID = None self.threadpoolShutdownID = None self.threadpool.stop() self.threadpool = None def getThreadPool(self): """ See L{twisted.internet.interfaces.IReactorThreads.getThreadPool}. """ if self.threadpool is None: self._initThreadPool() return self.threadpoolclass Server: def __init__(self): pass def request(self,params): sleep(0.5) print "Server: ",paramsclass Expiry: def __init__(self): pass def request(self,params): sleep(0.5) print "Renewal: ",params if __name__=="__main__": ########################## ##### first threadpool ### ########################## t1=Thread('t1') mainPool=t1.getThreadPool() mainPool.adjustPoolsize(maxthreads=2) ############################# ##### second threadpool ##### ############################# t2=Thread('t2') expiryPool=t2.getThreadPool() expiryPool.adjustPoolsize(maxthreads=10) print mainPool.max,mainPool.name print expiryPool.max,expiryPool.name for i in range(0,10): d=threads.deferToThreadPool(reactor, mainPool,Server().request,{"id":i}) for k in range(0,10): d=threads.deferToThreadPool(reactor, expiryPool,Expiry().request,{"id":k}) reactor.run()