Support for results that aren't immediately available.
Maintainer: Glyph Lefkowitz
| Class | |
Deferred debug helper. |
| Class | |
This is a callback which will be put off until later. |
| Class | |
A FilesystemLock that allows for a Deferred to be fired when the lock is acquired. |
| Class | |
DeferredList is a tool for collecting the results of several Deferreds. |
| Class | |
A lock for event driven systems. |
| Class | |
An event driven queue. |
| Class | |
A semaphore for event driven systems. |
| Exception | |
This error is raised when one of Deferred.callback or Deferred.errback is called after one of the two had already been called. |
| Exception | |
Raised when DeferredFilesystemLock.deferUntilLocked is called twice on a single DeferredFilesystemLock. |
| Exception | |
This error is raised by default when a Deferred is cancelled. |
| Exception | |
More than one failure occurred. |
| Exception | |
First error to occur in a DeferredList if fireOnOneErrback is set. |
| Exception | |
This error is raised when a coroutine is expected and something else is encountered. |
| Exception | |
Undocumented |
| Exception | |
Undocumented |
| Exception | |
This error is raised by default when a Deferred times out. |
| Function | ensure |
Schedule the execution of a coroutine that awaits/yields from Deferreds, wrapping it in a Deferred that will fire on success/failure of the coroutine. If a Deferred is passed to this function, it will be returned directly (mimicking the ... |
| Function | execute |
Create a Deferred from a callable and arguments. |
| Function | fail |
Return a Deferred that has already had .errback(result) called. |
| Function | gather |
Returns, via a Deferred, a list with the results of the given Deferreds - in effect, a "join" of multiple deferred operations. |
| Function | get |
Determine whether Deferred debugging is enabled. |
| Function | inline |
inlineCallbacks helps you write Deferred-using code that looks like a regular sequential function. For example: |
| Function | log |
Log and return failure. |
| Function | maybe |
Invoke a function that may or may not return a Deferred or coroutine. |
| Function | passthru |
Undocumented |
| Function | race |
Select the first available result from the sequence of Deferreds and cancel the rest. |
| Function | return |
Return val from a inlineCallbacks generator. |
| Function | set |
Enable or disable Deferred debugging. |
| Function | succeed |
Return a Deferred that has already had .callback(result) called. |
| Function | timeout |
Undocumented |
| Constant | FAILURE |
Undocumented |
| Constant | SUCCESS |
Undocumented |
| Type Alias | |
Undocumented |
| Type Alias | |
Undocumented |
| Variable | log |
Undocumented |
| Class | _ |
Cancellation status of an inlineCallbacks invocation. |
| Class | _ |
No class docstring; 0/1 instance variable, 2/7 methods documented |
| Class | _ |
No summary |
| Exception | _ |
Undocumented |
| Exception | _ |
A unique exception used only in _cancellableInlineCallbacks to verify that an inlineCallbacks is being cancelled as expected. |
| Function | _add |
Helper for _cancellableInlineCallbacks to add _handleCancelInlineCallbacks as the first errback. |
| Function | _cancellable |
Make an @inlineCallbacks cancellable. |
| Function | _cancelled |
A default translation function that translates Failures that are CancelledErrors to TimeoutErrors. |
| Function | _ |
Undocumented |
| Function | _failthru |
Undocumented |
| Function | _got |
Helper for _inlineCallbacks to handle a nested Deferred firing. |
| Function | _handle |
Propagate the cancellation of an @inlineCallbacks to the Deferred it is waiting on. |
| Function | _inline |
Carry out the work of inlineCallbacks. |
| Function | _parse |
Undocumented |
| Constant | _DEFERRED |
Undocumented |
| Constant | _NONE |
Undocumented |
| Constant | _P |
Undocumented |
| Type Variable | _ |
Undocumented |
| Type Variable | _ |
Undocumented |
| Type Variable | _T |
Undocumented |
| Type Alias | _ |
Undocumented |
| Type Alias | _ |
Undocumented |
| Type Alias | _ |
Undocumented |
| Type Alias | _ |
Undocumented |
| Type Alias | _ |
Undocumented |
| Type Alias | _ |
Undocumented |
| Variable | _old |
Undocumented |
Coroutine[ Deferred[ Any], Any, _T] | Generator[ Deferred[ Any], Any, _T] | Deferred[ _T]) -> Deferred[ _T]:
(source)
¶
Schedule the execution of a coroutine that awaits/yields from Deferreds, wrapping it in a Deferred that will fire on success/failure of the coroutine. If a Deferred is passed to this function, it will be returned directly (mimicking the asyncio.ensure_future function).
See Deferred.fromCoroutine for examples of coroutines.
| Parameters | |
coro:Coroutine[ | The coroutine object to schedule, or a Deferred. |
| Returns | |
Deferred[ | Undocumented |
Return a Deferred that has already had .errback(result) called.
See succeed's docstring for rationale.
| Parameters | |
result:Failure | BaseException | None | The same argument that Deferred.errback takes. |
| Returns | |
Deferred[ | Undocumented |
| Raises | |
NoCurrentExceptionError | If result is None but there is no current exception state. |
Iterable[ Deferred[ _T]], consumeErrors: bool = False) -> Deferred[ list[ _T]]:
(source)
¶
Returns, via a Deferred, a list with the results of the given Deferreds - in effect, a "join" of multiple deferred operations.
The returned Deferred will fire when all of the provided Deferreds have fired, or when any one of them has failed.
This method can be cancelled by calling the cancel method of the Deferred, all the Deferreds in the list will be cancelled.
This differs from DeferredList in that you don't need to parse the result for success/failure.
| Parameters | |
deferredIterable[ | Undocumented |
consumebool | (keyword param) a flag, defaulting to False, indicating that failures in any of the given Deferreds should not be propagated to errbacks added to the individual Deferreds after this gatherResults invocation. Any such errors in the individual Deferreds will be converted to a callback result of None. This is useful to prevent spurious 'Unhandled error in Deferred' messages from being logged. This parameter is available since 11.1.0. |
| Returns | |
Deferred[ | Undocumented |
Callable[ _P, Generator[ Deferred[ Any], Any, _T]]) -> Callable[ _P, Deferred[ _T]]:
(source)
¶
inlineCallbacks helps you write Deferred-using code that looks like a regular sequential function. For example:
@inlineCallbacks
def thingummy():
thing = yield makeSomeRequestResultingInDeferred()
print(thing) # the result! hoorj!
When you call anything that results in a Deferred, you can simply yield it; your generator will automatically be resumed when the Deferred's result is available. The generator will be sent the result of the Deferred with the 'send' method on generators, or if the result was a failure, 'throw'.
Things that are not Deferreds may also be yielded, and your generator will be resumed with the same object sent back. This means yield performs an operation roughly equivalent to maybeDeferred.
Your inlineCallbacks-enabled generator will return a Deferred object, which will result in the return value of the generator (or will fail with a failure object if your generator raises an unhandled exception). Inside the generator simply use return result to return a value.
Be aware that generator must not return a Deferred. If you believe the thing you'd like to return could be a Deferred, do this:
result = yield result
return result
The Deferred returned from your deferred generator may errback if your generator raised an exception:
@inlineCallbacks
def thingummy():
thing = yield makeSomeRequestResultingInDeferred()
if thing == 'I love Twisted':
# will become the result of the Deferred
return 'TWISTED IS GREAT!'
else:
# will trigger an errback
raise Exception('DESTROY ALL LIFE')
You can cancel the Deferred returned from your inlineCallbacks generator before it is fired by your generator completing (either by reaching its end, a return statement, or by calling returnValue). A CancelledError will be raised from the yielded Deferred that has been cancelled if that Deferred does not otherwise suppress it.
inlineCallbacks behaves very similarly to coroutines. Since Twisted 24.7.0 it is possible to rewrite functions using inlineCallbacks to async def in piecewise manner and be mostly compatible to existing code.
The rewrite process is simply replacing inlineCallbacks decorator with async def and all yield occurrences in the function body with await. The function will no longer return a Deferred but a awaitable coroutine. This return value will obviously not have Deferred methods such as addCallback, but it will be possible to yield it in other code based on inlineCallbacks.
Log and return failure.
This method can be used as an errback that passes the failure on to the next errback unmodified. Note that if this is the last errback, and the deferred gets garbage collected after being this errback has been called, the clean up code logs it again.
Invoke a function that may or may not return a Deferred or coroutine.
Call the given function with the given arguments. Then:
- If the returned object is a
Deferred, return it. - If the returned object is a
Failure, wrap it withfailand return it. - If the returned object is a
types.CoroutineType, wrap it withDeferred.fromCoroutineand return it. - Otherwise, wrap it in
succeedand return it. - If an exception is raised, convert it to a
Failure, wrap it infail, and then return it.
| Parameters | |
f:Callable[ | The callable to invoke |
*args:_P.args | The arguments to pass to f |
**kwargs:_P.kwargs | The keyword arguments to pass to f |
| Returns | |
Deferred[ | The result of the function call, wrapped in a Deferred if necessary. |
Select the first available result from the sequence of Deferreds and cancel the rest.
| Returns | |
Deferred[ | A cancellable Deferred that fires with the index and output of the element of ds to have a success result first, or that fires with FailureGroup holding a list of their failures if they all fail. |
Return val from a inlineCallbacks generator.
Note: this is currently implemented by raising an exception derived from BaseException. You might want to change any 'except:' clauses to an 'except Exception:' clause so as not to catch this exception.
Also: while this function currently will work when called from within arbitrary functions called from within the generator, do not rely upon this behavior.
Enable or disable Deferred debugging.
When debugging is on, the call stacks from creation and invocation are recorded, and added to any AlreadyCalledErrors we raise.
Return a Deferred that has already had .callback(result) called.
This is useful when you're writing synchronous code to an asynchronous interface: i.e., some code is calling you expecting a Deferred result, but you don't actually need to do anything asynchronous. Just return defer.succeed(theResult).
See fail for a version of this function that uses a failing Deferred rather than a successful one.
| Parameters | |
result:_T | The result to give to the Deferred's 'callback' method. |
| Returns | |
Deferred[ | Undocumented |
twisted.internet.defer.Deferred.addTimeout instead.Undocumented
Helper for _cancellableInlineCallbacks to add _handleCancelInlineCallbacks as the first errback.
| Parameters | |
it:Deferred[ | The Deferred to add the errback to. |
status:_CancellationStatus[ | a _CancellationStatus tracking the current status of gen |
Generator[ Deferred[ Any], object, _T] | Coroutine[ Deferred[ Any], object, _T]) -> Deferred[ _T]:
(source)
¶
Make an @inlineCallbacks cancellable.
| Parameters | |
gen:Generator[ | a generator object returned by calling a function or method decorated with @inlineCallbacks |
| Returns | |
Deferred[ | Deferred for the @inlineCallbacks that is cancellable. |
A default translation function that translates Failures that are CancelledErrors to TimeoutErrors.
| Parameters | |
value:_T | Anything |
timeout:float | The timeout |
| Returns | |
_T | Undocumented |
| Raises | |
TimeoutError | If value is a Failure that is a CancelledError. |
Exception | If value is a Failure that is not a CancelledError, it is re-raised. |
| Present Since | |
| 16.5 |
Iterable[ Deferred[ _SelfResultT]], fireOnOneCallback: Literal[ True], fireOnOneErrback: bool = False, consumeErrors: bool = False) -> Deferred[ _DeferredListSingleResultT[ _SelfResultT]]:Iterable[ Deferred[ _SelfResultT]], fireOnOneCallback: Literal[ False] = False, fireOnOneErrback: bool = False, consumeErrors: bool = False) -> Deferred[ _DeferredListResultListT[ _SelfResultT]]:Undocumented
object, waiting: list[ Any], gen: Generator[ Deferred[ Any], Any, _T] | Coroutine[ Deferred[ Any], Any, _T], status: _CancellationStatus[ _T], context: _Context):
(source)
¶
Helper for _inlineCallbacks to handle a nested Deferred firing.
| Parameters | |
r:object | The result of the Deferred |
waiting:list[ | Whether the _inlineCallbacks was waiting, and the result. |
gen:Generator[ | a generator object returned by calling a function or method decorated with @inlineCallbacks |
status:_CancellationStatus[ | a _CancellationStatus tracking the current status of gen |
context:_Context | the contextvars context to run `gen` in |
Failure, status: _CancellationStatus[ _T], /) -> Deferred[ _T]:
(source)
¶
Propagate the cancellation of an @inlineCallbacks to the Deferred it is waiting on.
| Parameters | |
result:Failure | An _InternalInlineCallbacksCancelledError from cancel(). |
status:_CancellationStatus[ | a _CancellationStatus tracking the current status of gen |
| Returns | |
Deferred[ | A new Deferred that the @inlineCallbacks generator can callback or errback through. |
object, gen: Generator[ Deferred[ Any], Any, _T] | Coroutine[ Deferred[ Any], Any, _T], status: _CancellationStatus[ _T], context: _Context):
(source)
¶
Carry out the work of inlineCallbacks.
Iterate the generator produced by an @inlineCallbacks-decorated function, gen, send()ing it the results of each value yielded by that generator, until a Deferred is yielded, at which point a callback is added to that Deferred to call this function again.
| Parameters | |
result:object | The last result seen by this generator. Note that this is never a Deferred - by the time this function is invoked, the Deferred has been called back and this will be a particular result at a point in its callback chain. |
gen:Generator[ | a generator object returned by calling a function or method decorated with @inlineCallbacks |
status:_CancellationStatus[ | a _CancellationStatus tracking the current status of gen |
context:_Context | the contextvars context to run `gen` in |
list[ _DeferredListResultItemT[ _T]], fireOnOneErrback: bool = False, /) -> list[ _T]:
(source)
¶
Undocumented
Undocumented
| Value |
|