twisted.internet.deferView 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/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
|Instance Variable||paused||A counter of how many unmatched
|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||addTimeout||Time out this
|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
|Class Method||fromFuture||Adapt an
|Instance Variable||_suppressAlreadyCalled||A flag used by the cancellation mechanism which is
|Instance Variable||_runningCallbacks||A flag which is
|Instance Variable||_chainedTo||If this
|Method||_continuation||Build a tuple of callback and errback with
|Method||_runCallbacks||Run the chain of callbacks once a result is available.|
errbackis called and afterwards always
pausecalls have been made on this instance.
Trueif the Deferred has no canceller and has been cancelled,
True, it can be expected that
errbackwill eventually be called and the result should be silently discarded.
|Parameters||canceller||a callable used to stop the pending operation scheduled by this |
Note that if a canceller is not given,
Convenience method for adding just a callback.
Convenience method for adding just an errback.
Convenience method for adding a single callable as both a callback and an errback.
Time out this
Deferred by scheduling it to be cancelled after
|Parameters||timeout||number of seconds to wait before timing out this |
|clock||The object which will be used to schedule the timeout. (type: |
|onTimeoutCancel||A callable which is called immediately after this |
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:
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
Deferreds together with
Run all success 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'. 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 will be chained to it (and further callbacks will not run until that
Deferred has a result).
|Parameters||result||The object which will be passed to the first callback added to this |
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 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.
Deferred has not yet had its
callback method invoked, call the canceller function provided to the constructor. If that function does not invoke
errback, or if no canceller function was provided, errback with
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.
_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.
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 |
Return a string representation of this
|Parameters||loop||The asyncio event loop to bind the |
|deferred||The Deferred to adapt. (type: |
|Returns||A Future which will fire when the Deferred fires. (type: |
|Present Since||Twisted 17.5.0|
|Note||converting a |
|Parameters||future||The Future to adapt. (type: |
|Returns||A Deferred which will fire when the Future fires. (type: |
|Present Since||Twisted 17.5.0|
|Note||This creates a |