def _runWithCallback(self, callback, errback, func, args, kwargs): try: result = apply(func, args, kwargs) except Exception, e: task.schedule(errback, e)
tpool = ThreadDispatcher() main.addShutdown(tpool.stop) """ def __init__(self, *args, **kwargs): apply(threadpool.ThreadPool.__init__, (self,) + args, kwargs) self._callbacks = [] def _runWithCallback(self, callback, errback, func, args, kwargs): try: result = apply(func, args, kwargs) except Exception, e: task.schedule(errback, e) else: task.schedule(callback, result) def dispatchWithCallback(self, owner, callback, errback, func, *args, **kw): """Dispatch a function, returning the result to a callback function. The callback function will be called in the main event loop thread. """ self.dispatchApply(owner, callback, errback, func, args, kw) def dispatchApply(self, owner, callback, errback, func, args, kw): self.dispatch(owner, self._runWithCallback, callback, errback, func, args, kw) theDispatcher = ThreadDispatcher(0) def dispatchApply(callback, errback, func, args, kw):
from twisted.internet import threadtask tpool = ThreadDispatcher() main.addShutdown(tpool.stop) """ def __init__(self, *args, **kwargs): apply(threadpool.ThreadPool.__init__, (self, ) + args, kwargs) self._callbacks = [] def _runWithCallback(self, callback, errback, func, args, kwargs): try: result = apply(func, args, kwargs) except Exception, e: task.schedule(errback, e) else: task.schedule(callback, result) def dispatchWithCallback(self, owner, callback, errback, func, *args, **kw): """Dispatch a function, returning the result to a callback function. The callback function will be called in the main event loop thread. """ self.dispatchApply(owner, callback, errback, func, args, kw) def dispatchApply(self, owner, callback, errback, func, args, kw): self.dispatch(owner, self._runWithCallback, callback, errback, func, args, kw) theDispatcher = ThreadDispatcher(0)