def addCallbacks(self, callback, errback=None, callbackArgs=None, callbackKeywords=None, errbackArgs=None, errbackKeywords=None): """ Add a pair of callbacks (success and error) to this L{Deferred}.
These will be executed when the 'master' callback is run.
@return: C{self}. @rtype: a L{Deferred} """ assert callable(callback) assert errback is None or callable(errback) cbs = ((callback, callbackArgs, callbackKeywords), (errback or (passthru), errbackArgs, errbackKeywords)) self.callbacks.append(cbs)
@failure._extraneous def _inlineCallbacks(result, g, status): waiting = [True, # waiting for result? None] # result
while 1: try: # Send the last result back as the result of the yield expression. isFailure = isinstance(result, failure.Failure) if isFailure: result = result.throwExceptionIntoGenerator(g) else: result = g.send(result) except StopIteration as e: # fell off the end, or "return" statement status.deferred.callback(getattr(e, "value", None)) return except _DefGen_Return as e: ...忽略... except: status.deferred.errback() return # 如果是defferd对象,递归调用 if isinstance(result, Deferred): # a deferred was yielded, get the result. def gotResult(r): if waiting[0]: waiting[0] = False waiting[1] = r else: # We are not waiting for deferred result any more _inlineCallbacks(r, g, status)
result.addBoth(gotResult) if waiting[0]: # Haven't called back yet, set flag so that we get reinvoked # and return from the loop waiting[0] = False status.waitingOn = result return