Go to the latest version of this document.
search in docstrings

Search bar offers the following options:

- Term presence. The below example searches for documents that must contain “foo”, might contain “bar” and must not contain “baz”: `+foo bar -baz`
- Wildcards. The below example searches for documents with words beginning with “foo”: `foo*`
- Search in specific fields. The following search matches all objects in "twisted.mail" that matches “search”: `+qname:twisted.mail.* +search`

  Possible fields: 'name', 'qname' (fully qualified name), 'docstring', and 'kind'. Last two fields are only applicable if "search in docstrings" is enabled.

- Fuzzy matches. The following search matches all documents that have a word within 1 edit distance of “foo”: `foo~1`

Results provided by Lunr.js

# ``twisted`.`internet`.`defer``

module documentation
(source)

Support for results that aren't immediately available.

Maintainer: Glyph Lefkowitz

Class | `DebugInfo` | Deferred debug helper.
Class | `Deferred` | This is a callback which will be put off until later.
Class | `DeferredFilesystemLock` | A `FilesystemLock` that allows for a `Deferred` to be fired when the lock is acquired.
Class | `DeferredList` | `DeferredList` is a tool for collecting the results of several Deferreds.
Class | `DeferredLock` | A lock for event driven systems.
Class | `DeferredQueue` | An event driven queue.
Class | `DeferredSemaphore` | A semaphore for event driven systems.
Class | `waitForDeferred` | See `deferredGenerator`.
Exception | `AlreadyCalledError` | This error is raised when one of `Deferred.callback` or `Deferred.errback` is called after one of the two had already been called.
Exception | `AlreadyTryingToLockError` | Raised when `DeferredFilesystemLock.deferUntilLocked` is called twice on a single `DeferredFilesystemLock`.
Exception | `CancelledError` | This error is raised by default when a `Deferred` is cancelled.
Exception | `FirstError` | First error to occur in a `DeferredList` if `fireOnOneErrback` is set.
Exception | `NotACoroutineError` | This error is raised when a coroutine is expected and something else is encountered.
Exception | `QueueOverflow` | Undocumented
Exception | `QueueUnderflow` | Undocumented
Exception | `TimeoutError` | This error is raised by default when a `Deferred` times out.
Function | `deferredGenerator` | `deferredGenerator` and `waitForDeferred` help you write `Deferred`-using code that looks like a regular sequential function. Consider the use of `inlineCallbacks` instead, which can accomplish the same thing in a more concise manner.
Function | `ensureDeferred` | Schedule the execution of a coroutine that awaits/yields from `Deferred`s, 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 (mimicing the ...
Function | `execute` | Create a `Deferred` from a callable and arguments.
Function | `fail` | Return a `Deferred` that has already had `.errback(result)` called.
Function | `gatherResults` | Returns, via a `Deferred`, a list with the results of the given `Deferred`s - in effect, a "join" of multiple deferred operations.
Function | `getDebugging` | Determine whether `Deferred` debugging is enabled.
Function | `inlineCallbacks` | `inlineCallbacks` helps you write `Deferred`-using code that looks like a regular sequential function. For example:
Function | `logError` | Log and return failure.
Function | `maybeDeferred` | Invoke a function that may or may not return a `Deferred`.
Function | `passthru` | Undocumented
Function | `returnValue` | Return val from a `inlineCallbacks` generator.
Function | `setDebugging` | 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 | `DeferredCallback` | Undocumented
Type Alias | `DeferredErrback` | Undocumented
Variable | `log` | Undocumented
Class | `_CancellationStatus` | Cancellation status of an `inlineCallbacks` invocation.
Class | `_ConcurrencyPrimitive` | No class docstring; 0/1 instance variable, 2/7 methods documented
Class | `_Sentinel` | No summary
Exception | `_DefGen_Return` | Undocumented
Exception | `_InternalInlineCallbacksCancelledError` | A unique exception used only in `_cancellableInlineCallbacks` to verify that an `inlineCallbacks` is being cancelled as expected.
Function | `_cancellableInlineCallbacks` | Make an `@``inlineCallbacks` cancellable.
Function | `_cancelledToTimedOutError` | A default translation function that translates `Failure`s that are `CancelledError`s to `TimeoutError`s.
Function | `_deferGenerator` | See `deferredGenerator`.
Function | `_DeferredList` | Undocumented
Function | `_failthru` | Undocumented
Function | `_inlineCallbacks` | Carry out the work of `inlineCallbacks`.
Function | `_parseDeferredListResult` | Undocumented
Constant | `_NONE_KWARGS` | Undocumented
Type Variable | `_ConcurrencyPrimitiveT` | Undocumented
Type Variable | `_DeferredLockT` | Undocumented
Type Variable | `_DeferredResultT` | Undocumented
Type Variable | `_DeferredSemaphoreT` | Undocumented
Type Variable | `_NextDeferredResultT` | Undocumented
Type Variable | `_T` | Undocumented
Type Alias | `_CallbackChain` | Undocumented
Type Alias | `_CallbackKeywordArguments` | Undocumented
Type Alias | `_CallbackOrderedArguments` | Undocumented
Type Alias | `_DeferableGenerator` | Undocumented
Type Alias | `_DeferredListResultItemT` | Undocumented
Type Alias | `_DeferredListResultListT` | Undocumented
Type Alias | `_DeferredListSingleResultT` | Undocumented
Variable | `_contextvarsSupport` | Undocumented

def deferredGenerator(f): (source)
Deprecated since version 15.0.0: `deferredGenerator` was deprecated in Twisted 15.0.0; please use `twisted.internet.defer.inlineCallbacks` instead.

`deferredGenerator` and `waitForDeferred` help you write `Deferred`-using code that looks like a regular sequential function. Consider the use of `inlineCallbacks` instead, which can accomplish the same thing in a more concise manner.

There are two important functions involved: `waitForDeferred`, and `deferredGenerator`. They are used together, like this:

        @deferredGenerator
        def thingummy():
            thing = waitForDeferred(makeSomeRequestResultingInDeferred())
            yield thing
            thing = thing.getResult()
            print(thing) #the result! hoorj!

`waitForDeferred` returns something that you should immediately yield; when your generator is resumed, calling `thing.getResult()` will either give you the result of the `Deferred` if it was a success, or raise an exception if it was a failure. Calling `getResult` is absolutely mandatory. If you do not call it, your program will not work.

`deferredGenerator` takes one of these waitForDeferred-using generator functions and converts it into a function that returns a `Deferred`. The result of the `Deferred` will be the last value that your generator yielded unless the last value is a `waitForDeferred` instance, in which case the result will be `None`. If the function raises an unhandled exception, the `Deferred` will errback instead. Remember that `return result` won't work; use `yield result; return` in place of that.

Note that not yielding anything from your generator will make the `Deferred` result in `None`. Yielding a `Deferred` from your generator is also an error condition; always yield `waitForDeferred(d)` instead.

The `Deferred` returned from your deferred generator may also errback if your generator raised an exception. For example:

        @deferredGenerator
        def thingummy():
            thing = waitForDeferred(makeSomeRequestResultingInDeferred())
            yield thing
            thing = thing.getResult()
            if thing == 'I love Twisted':
                # will become the result of the Deferred
                yield 'TWISTED IS GREAT!'
                return
            else:
                # will trigger an errback
                raise Exception('DESTROY ALL LIFE')

Put succinctly, these functions connect deferred-using code with this 'fake blocking' style in both directions: `waitForDeferred` converts from a `Deferred` to the 'blocking' style, and `deferredGenerator` converts from the 'blocking' style to a `Deferred`.

Parameters
f:`Callable[..., _DeferableGenerator]` | Undocumented
Returns
`Callable[..., Deferred[object]]` | Undocumented

def ensureDeferred(coro): (source)

Schedule the execution of a coroutine that awaits/yields from `Deferred`s, 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 (mimicing the `asyncio.ensure_future` function).

See `Deferred.fromCoroutine` for examples of coroutines.

Parameters
coro:`Union[Coroutine[Deferred[_T], Any, _T], Generator[Deferred[_T], Any, _T], Deferred[_T]]` | The coroutine object to schedule, or a `Deferred`.
Returns
`Deferred[_T]` | Undocumented

def execute(callable, *args, **kwargs): (source)

Create a `Deferred` from a callable and arguments.

Call the given function with the given arguments. Return a `Deferred` which has been fired with its callback as the result of that invocation or its `errback` with a `Failure` for the exception thrown.

Parameters
callable:`Callable[..., _T]` | Undocumented
*args:`object` | Undocumented
**kwargs:`object` | Undocumented
Returns
`Deferred[_T]` | Undocumented

def fail(result=None): (source)

Return a `Deferred` that has already had `.errback(result)` called.

See `succeed`'s docstring for rationale.

Parameters
result:`Optional[Union[Failure, BaseException]]` | The same argument that `Deferred.errback` takes.
Returns
`Deferred[Any]` | Undocumented
Raises
`NoCurrentExceptionError` | If `result` is `None` but there is no current exception state.

def gatherResults(deferredList, consumeErrors=False): (source)

Returns, via a `Deferred`, a list with the results of the given `Deferred`s - in effect, a "join" of multiple deferred operations.

The returned `Deferred` will fire when all of the provided `Deferred`s 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 `Deferred`s in the list will be cancelled.

This differs from `DeferredList` in that you don't need to parse the result for success/failure.

Parameters
deferredList:`Iterable[Deferred[_T]]` | Undocumented

consumeErrors:`bool`

(keyword param) a flag, defaulting to False, indicating that failures in any of the given `Deferred`s should not be propagated to errbacks added to the individual `Deferred`s after this `gatherResults` invocation. Any such errors in the individual `Deferred`s 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[List[_T]]` | Undocumented

def getDebugging(): (source)

Determine whether `Deferred` debugging is enabled.

Returns
`bool` | Undocumented

def inlineCallbacks(f): (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 `Deferred`s 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). Note that you can't use `return result` to return a value; use `returnValue(result)` instead. Falling off the end of the generator, or simply using `return` will cause the `Deferred` to have a result of `None`.

Be aware that `returnValue` will not accept a `Deferred` as a parameter. If you believe the thing you'd like to return could be a `Deferred`, do this:

        result = yield result
        returnValue(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
                returnValue('TWISTED IS GREAT!')
            else:
                # will trigger an errback
                raise Exception('DESTROY ALL LIFE')

It is possible to use the `return` statement instead of `returnValue`:

        @inlineCallbacks
        def loadData(url):
            response = yield makeRequest(url)
            return json.loads(response)

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 `yield`ed `Deferred` that has been cancelled if that `Deferred` does not otherwise suppress it.

Parameters
f:`Callable[..., Generator[Deferred[object], object, _T]]` | Undocumented
Returns
`Callable[..., Deferred[_T]]` | Undocumented

def logError(err): (source)

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.

Parameters
err:`Failure` | Undocumented
Returns
`Failure` | Undocumented

def maybeDeferred(f, *args, **kwargs): (source)

Invoke a function that may or may not return a `Deferred`.

Call the given function with the given arguments. If the returned object is a `Deferred`, return it. If the returned object is a `Failure`, wrap it with `fail` and return it. Otherwise, wrap it in `succeed` and return it. If an exception is raised, convert it to a `Failure`, wrap it in `fail`, and then return it.

Parameters
f:`Callable[..., _T]` | The callable to invoke
*args:`object` | The arguments to pass to `f`
**kwargs:`object` | The keyword arguments to pass to `f`
Returns
`Deferred[_T]` | The result of the function call, wrapped in a `Deferred` if necessary.

def passthru(arg): (source)

Undocumented

Parameters
arg:`_T` | Undocumented
Returns
`_T` | Undocumented

def returnValue(val): (source)

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.

Parameters
val:`object` | Undocumented
Returns
`NoReturn` | Undocumented

def setDebugging(on): (source)

Enable or disable `Deferred` debugging.

When debugging is on, the call stacks from creation and invocation are recorded, and added to any `AlreadyCalledError`s we raise.

Parameters
on:`bool` | Undocumented

def succeed(result): (source)

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[_T]` | Undocumented

def timeout(deferred): (source)
Deprecated since version 17.1.0: `timeout` was deprecated in Twisted 17.1.0; please use `twisted.internet.defer.Deferred.addTimeout` instead.

Undocumented

Parameters
deferred:`Deferred[object]` | Undocumented

FAILURE: `bool` = (source)

Undocumented

Value

    False

SUCCESS: `bool` = (source)

Undocumented

Value

    True

DeferredCallback = (source)

Undocumented

Value

    Callable[..., object]

DeferredErrback = (source)

Undocumented

Value

    Callable[..., object]

log = (source)

Undocumented

def _cancellableInlineCallbacks(gen): (source)

Make an `@``inlineCallbacks` cancellable.

Parameters
gen:`Union[Generator[Deferred[_T], object, _T], Coroutine[Deferred[_T], object, _T]]` | a generator object returned by calling a function or method decorated with `@``inlineCallbacks`
Returns
`Deferred[_T]` | `Deferred` for the `@``inlineCallbacks` that is cancellable.

def _cancelledToTimedOutError(value, timeout): (source)

A default translation function that translates `Failure`s that are `CancelledError`s to `TimeoutError`s.

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

def _deferGenerator(g, deferred): (source)

See `deferredGenerator`.

Parameters
g:`_DeferableGenerator` | Undocumented
deferred:`Deferred[object]` | Undocumented
Returns
`Deferred[Any]` | Undocumented

def _DeferredList(deferredList, fireOnOneCallback=False, fireOnOneErrback=False, consumeErrors=False): (source)

Undocumented

Parameters
deferredList:`Iterable[Deferred[_DeferredResultT]]` | Undocumented
fireOnOneCallback:`bool` | Undocumented
fireOnOneErrback:`bool` | Undocumented
consumeErrors:`bool` | Undocumented
Returns
`Union[Deferred[_DeferredListSingleResultT], Deferred[_DeferredListResultListT]]` | Undocumented

def _failthru(arg): (source)

Undocumented

Parameters
arg:`Failure` | Undocumented
Returns
`Failure` | Undocumented

@_extraneous
def _inlineCallbacks(result, gen, status): (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 `yield`ed 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:`Union[Generator[Deferred[_T], object, None], Coroutine[Deferred[_T], object, None]]` | a generator object returned by calling a function or method decorated with `@``inlineCallbacks`
status:`_CancellationStatus` | a `_CancellationStatus` tracking the current status of `gen`

def _parseDeferredListResult(resultList, fireOnOneErrback=False): (source)

Undocumented

Parameters
resultList:`List[_DeferredListResultItemT]` | Undocumented
fireOnOneErrback:`bool` | Undocumented
Returns
`List[_T]` | Undocumented

_NONE_KWARGS: `_CallbackKeywordArguments` = (source)

Undocumented

Value

    MappingProxyType({})

_ConcurrencyPrimitiveT = (source)

Undocumented

Value

    TypeVar('_ConcurrencyPrimitiveT',
            bound='_ConcurrencyPrimitive')

_DeferredLockT = (source)

Undocumented

Value

    TypeVar('_DeferredLockT',
            bound='DeferredLock')

_DeferredResultT = (source)

Undocumented

Value

    TypeVar('_DeferredResultT',
            contravariant=True)

_DeferredSemaphoreT = (source)

Undocumented

Value

    TypeVar('_DeferredSemaphoreT',
            bound='DeferredSemaphore')

_NextDeferredResultT = (source)

Undocumented

Value

    TypeVar('_NextDeferredResultT',
            covariant=True)

_T = (source)

Undocumented

Value

    TypeVar('_T')

_CallbackChain = (source)

Undocumented

Value

    Tuple[Tuple[Union[DeferredCallback, Literal[_Sentinel._CONTINUE]],
                _CallbackOrderedArguments,
                _CallbackKeywordArguments],
          Tuple[Union[DeferredErrback,
                      DeferredCallback,
                      Literal[_Sentinel._CONTINUE]],
                _CallbackOrderedArguments,
    ...

_CallbackKeywordArguments = (source)

Undocumented

Value

    Mapping[str, object]

_CallbackOrderedArguments = (source)

Undocumented

Value

    Tuple[object, ...]

_DeferableGenerator = (source)

Undocumented

Value

    Generator[object, None, None]

_DeferredListResultItemT = (source)

Undocumented

Value

    Tuple[bool, _DeferredResultT]

_DeferredListResultListT = (source)

Undocumented

Value

    List[_DeferredListResultItemT]

_DeferredListSingleResultT = (source)

Undocumented

Value

    Tuple[_DeferredResultT, int]

_contextvarsSupport: `bool` = (source)

Undocumented