def _complete(self, out): assert not BaseTask._cleanup_running, "Discontinuing processing because cleanup has started." msg(consoleformat="%(task)r._complete(%(result)r)", task=self, result=out) if isinstance(out, failure.Failure): try: self._fail(out) self.deferred.pause() BaseTask._all_cleanup() reactor.callFromThread(reactor.stop) return out except: err(failure.Failure()) if self.deferred.callbacks: self.deferred.addBoth(self._complete) return out parent = getattr(self, 'parent', None) try: if parent: parent.complete_sub(out, self) finally: if self in self.__bg_tasks: self.__bg_tasks.remove(self) if not self.__bg_tasks and reactor.running: reactor.stop() return out
def background_task(func, self, *args, **kwargs): self._register() msg(consoleformat=u"%(obj)r.%(fun)s queue", obj=self, fun=func.func_name) reactor.callWhenRunning(func, self, *args, **kwargs) if not reactor.running: msg(consoleformat=u"Starting %(task)r", task=self) reactor.run() sys.exit()
def processEnded(self, reason): (self.task.out, self.task.err) = map(''.join, (self._out, self._err)) msg(consoleformat="process exited with reason %(reason)r, output %(output)r", reason=reason, output=(self.task.out, self.task.err)) if reason.check(error.ProcessDone) and not (reason.value.status or reason.value.signal): self.task.deferred.callback((self.task.out, self.task.err)) else: reason._taskoutput = ''.join(self._all) self.task.deferred.errback(reason)
def _all_cleanup(cls): cls._cleanup_running = True for task in cls.__bg_tasks: msg(consoleformat="cleaning %(task)r", task=task) try: task.deferred.pause() task._cleanup() except: err(failure.Failure())
def __init__(self, *args, **kwargs): self.args = args _id = kwargs.get('_id') if _id: self.__id = "%s@%x" % (_id, id(self)) else: self.__id = "@%x" % id(self) msg(consoleformat=u"new Task %(obj)r: %(obj)s", obj=self) self.deferred = defer.Deferred() self.deferred.addBoth(self._complete)
def logfunc(func, self, *args, **kwargs): msg(consoleformat=u"%(obj)r.%(fun)s start", obj=self, fun=func.func_name) try: return func(self, *args, **kwargs) finally: msg(consoleformat=u"%(obj)r.%(fun)s finish", obj=self, fun=func.func_name)
def queue_task(func, self, *args, **kwargs): self._register() msg(consoleformat=u"%(obj)r.%(fun)s queue", obj=self, fun=func.func_name) reactor.callWhenRunning(func, self, *args, **kwargs)
def run_sub(self, sub, *args, **kwargs): msg(consoleformat="start %(sub)r for %(obj)r", sub=sub, obj=self) self.subs.add(sub) sub.parent = self sub.run(*args, **kwargs)