Part of twisted.internet.defer View Source View In Hierarchy
This is a callback which will be put off until later.
Why do we want this? Well, in cases where a function in a threaded
program would block until it gets a result, for Twisted it should not
block. Instead, it should return a
This can be implemented for protocols that run over the network by
writing an asynchronous protocol for
twisted.internet. For methods
that come from outside packages that are not under our control, we use
threads (see for example
For more information about Deferreds, see doc/howto/core/defer.html or http://twistedmatrix.com/documents/current/core/howto/defer.htmlWhen creating a Deferred, you may provide a canceller function, which will be called by d.cancel() to let you do any clean-up necessary if the user decides not to wait for the deferred to complete.
|Instance Variables||called||A flag which is |
|paused||A counter of how many unmatched |
|_suppressAlreadyCalled||A flag used by the cancellation mechanism which is |
|_runningCallbacks||A flag which is |
|_chainedTo||If this Deferred is waiting for the result of another Deferred, this is
a reference to the other Deferred. Otherwise, |
|Method||addCallbacks||Add a pair of callbacks (success and error) to this
|Method||addCallback||Convenience method for adding just a callback.|
|Method||addErrback||Convenience method for adding just an errback.|
|Method||addBoth||Convenience method for adding a single callable as both a callback and an errback.|
|Method||callback||Run all success callbacks that have been added to this
|Method||errback||Run all error callbacks that have been added to this
|Method||pause||Stop processing on a
|Method||unpause||Process all callbacks made since
|Method||__str__||Return a string representation of this
|Method||_continuation||Build a tuple of callback and errback with
|Method||_runCallbacks||Run the chain of callbacks once a result is available.|
Add a pair of callbacks (success and error) to this
Convenience method for adding just a callback.See
Convenience method for adding just an errback.See
Convenience method for adding a single callable as both a callback and an errback.See
d's callback or errback, as appropriate. It is merely a shorthand way of performing the following:
When you chain a deferred d2 to another deferred d1 with d1.chainDeferred(d2), you are making d2 participate in the callback chain of d1. Thus any event that fires d1 will also fire d2. However, the converse is not true; if d2 is fired d1 will not be affected.Note that unlike the case where chaining is caused by a
Deferredbeing returned from a callback, it is possible to cause the call stack size limit to be exceeded by chaining many
Deferreds together with
Run all success callbacks that have been added to this
Failureor raises an
Exception, processing will continue on the *error* callback chain. If a callback (or errback) returns another
Deferredwill be chained to it (and further callbacks will not run until that
Deferredhas a result).
Run all error callbacks that have been added to this
Each callback will have its result passed as the first argument to the
next; this way, the callbacks act as a 'processing chain'. Also, if the
error-callback returns a non-Failure or doesn't raise an
Exception, processing will continue on the *success*-callback
If the argument that's passed to me is not a
instance, it will be embedded in one. If no argument is passed, a
instance will be created based on the current traceback stack.
not yet had its
invoked, call the canceller function provided to the constructor. If that
function does not invoke
if no canceller function was provided, errback with
Deferredis waiting on another
Deferred, forward the cancellation to the other
_continueto be used by
_removeContinueon another Deferred.
Run the chain of callbacks once a result is available.
This consists of a simple loop over all of the callbacks, calling each with the current result and making the current result equal to the return value (or raised exception) of that call.
self._runningCallbacks is true, this loop won't run at
all, since it is already running above us on the call stack. If
self.paused is true, the loop also won't run, because that's
what it means to be paused.
The loop will terminate before processing all of the callbacks if a
Deferred without a result is encountered.
Deferredwith a result is encountered, that result is taken and the loop proceeds.
|Note||The implementation is complicated slightly by the fact that chaining (associating two Deferreds with each other such that one will wait for the result of the other, as happens when a Deferred is returned from a callback on another Deferred) is supported iteratively rather than recursively, to avoid running out of stack frames when processing long chains.|