@@ -0,0 +1,1825 @@
+class Task(object):%0A%0A    def __init__(self, callback):%0A        self._callback = callback%0A        self.is_done = True%0A        self._cleanup = %5B%5D%0A%0A    def __setattr__(self, name, value):%0A        if name == 'cleanup':%0A            self._cleanup.append(value)%0A        else:%0A            super(Task, self).__setattr__(name, value)%0A%0A    @property%0A    def callback(self):%0A        for cleanup in self._cleanup:%0A            cleanup()%0A        return self._callback%0A%0A    def call(self, fn, args=None, kwargs=None, on_success=None, on_error=None, on_timeout=None):%0A        self.is_done = False%0A%0A        def cb(rc, result):%0A            _callback(self, rc, result, on_success, on_error, on_timeout)%0A            self.is_done = True%0A%0A        if args is None:%0A            args = ()%0A        elif not isinstance(args, (tuple, list)):%0A            args = (args,)%0A%0A        if kwargs is None:%0A            kwargs = %7B%7D%0A%0A        fn(cb, *args, **kwargs)%0A%0A%0Adef _callback(task, rc, result, on_success, on_error, on_timeout):%0A    if rc == 0:%0A        if on_success:%0A            try:%0A                return on_success(task, result)%0A            except Exception as e:%0A                return task.callback(1, 'exception during on_success: %25s' %25 e)%0A    else:%0A        if on_timeout and result == 'timeout':%0A            try:%0A                return on_timeout(task, result)%0A            except Exception as e:%0A                return task.callback(1, 'exception during on_timeout: %25s' %25 e)%0A        if on_error:%0A            try:%0A                return on_error(task, result)%0A            except Exception as e:%0A                return task.callback(1, 'exception during on_error: %25s' %25 e)%0A    task.callback(rc, result)%0A%0A%0Adef callback_as_task(fn):%0A    def inner(callback, *args, **kwargs):%0A        task = Task(callback)%0A        fn(task, *args, **kwargs)%0A    return inner%0A

add task handler