Part of twisted.internet.defer View Source View In Hierarchy
Known subclasses: twisted.internet.defer.DeferredList, twisted.persisted.crefutil._Defer
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 Deferred
.
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 twisted.enterprise.adbapi
).
For more information about Deferreds, see doc/core/howto/defer.html or http://twistedmatrix.com/documents/current/core/howto/defer.html
When 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 Variable | called | A flag which is False until either callback or
errback is called and afterwards always True . (type: bool ) |
Instance Variable | paused | A counter of how many unmatched pause calls have been made on
this instance. (type: int ) |
Method | __init__ | Initialize a Deferred . |
Method | addCallbacks | Add a pair of callbacks (success and error) to this Deferred . |
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 | chainDeferred | Chain another Deferred to
this Deferred . |
Method | callback | Run all success callbacks that have been added to this Deferred . |
Method | errback | Run all error callbacks that have been added to this Deferred . |
Method | pause | Stop processing on a Deferred until
unpause ()
is called. |
Method | unpause | Process all callbacks made since pause ()
was called. |
Method | cancel | Cancel this Deferred . |
Method | __str__ | Return a string representation of this Deferred . |
Instance Variable | _suppressAlreadyCalled | A flag used by the cancellation mechanism which is True if the
Deferred has no canceller and has been cancelled, False
otherwise. If True , it can be expected that
callback or errback will eventually be called and
the result should be silently discarded. (type: bool ) |
Instance Variable | _runningCallbacks | A flag which is True while this instance is executing its
callback chain, used to stop recursive execution of _runCallbacks (type: bool ) |
Instance Variable | _chainedTo | If this Deferred is waiting for the result of another Deferred, this is a
reference to the other Deferred. Otherwise, None . |
Method | _startRunCallbacks | Undocumented |
Method | _continuation | Build a tuple of callback and errback with _continue to be
used by _addContinue and _removeContinue on
another Deferred. |
Method | _runCallbacks | Run the chain of callbacks once a result is available. |
False
until either callback
or
errback
is called and afterwards always True
. (type: bool
)
True
if the
Deferred has no canceller and has been cancelled, False
otherwise. If True
, it can be expected that
callback
or errback
will eventually be called and
the result should be silently discarded. (type: bool
)
True
while this instance is executing its
callback chain, used to stop recursive execution of _runCallbacks
(type: bool
)
None
.
Deferred
.Parameters | canceller | a callable used to stop the pending operation scheduled by this Deferred when
Deferred.cancel
is invoked. The canceller will be passed the deferred whose cancelation is
requested (i.e., self).
If a canceller is not given, or does not invoke its argument's
Note that if a canceller is not given, Deferred . The
return result is ignored.) |
See addCallbacks
.
See addCallbacks
.
See addCallbacks
.
Deferred
to
this Deferred
.
This method adds callbacks to this Deferred
to
call d
's callback or errback, as appropriate. It is merely a
shorthand way of performing the following:
self.addCallbacks(d.callback, d.errback)
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 Deferred
being
returned from a callback, it is possible to cause the call stack size limit
to be exceeded by chaining many Deferred
s
together with chainDeferred
.
Returns | self . (type: a Deferred ) |
Deferred
.
Each callback will have its result passed as the first argument to the
next; this way, the callbacks act as a 'processing chain'. If the
success-callback returns a Failure
or
raises an Exception
,
processing will continue on the *error* callback chain. If a callback (or
errback) returns another Deferred
, this
Deferred
will be chained to it (and further callbacks will not run until that Deferred
has a
result).
An instance of Deferred
may
only have either callback
or errback
called on it, and only once.
Parameters | result | The object which will be passed to the first callback added to this Deferred (via
addCallback ). |
Raises | AlreadyCalledError | If callback
or errback
has already been called on this Deferred . |
Deferred
.
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 chain.
If the argument that's passed to me is not a failure.Failure
instance, it will be embedded in one. If no argument is passed, a failure.Failure
instance will be created based on the current traceback stack.
Passing a string as `fail' is deprecated, and will be punished with a warning message.
An instance of Deferred
may
only have either callback
or errback
called on it, and only once.
Parameters | fail | The Failure
object which will be passed to the first errback added to this Deferred (via
addErrback ).
Alternatively, a Exception
instance from which a Failure will be
constructed (with no traceback) or None to create a Failure
instance from the current exception state (with a traceback). |
Raises | AlreadyCalledError | If callback
or errback
has already been called on this Deferred . |
NoCurrentExceptionError | If fail is None but there is no current exception
state. |
Deferred
.
If the Deferred
has
not yet had its errback
or callback
method
invoked, call the canceller function provided to the constructor. If that
function does not invoke callback
or errback
, or
if no canceller function was provided, errback with CancelledError
.
If this Deferred
is
waiting on another Deferred
,
forward the cancellation to the other Deferred
.
_continue
to be
used by _addContinue
and _removeContinue
on
another Deferred.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.
If 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.
If a Deferred
with 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. |