[Twisted-Python] why can't a callback be called with a deferred?

Adi Roiban adi at roiban.ro
Tue Feb 19 04:41:37 MST 2019


On Tue, 19 Feb 2019 at 11:01, Chris Withers <chris at withers.org> wrote:
>
> Hi All,
>
> There's this assert:
>
> https://github.com/twisted/twisted/blob/trunk/src/twisted/internet/defer.py#L459
>
> ...and I'd like to understand why it's there.

[snip]

> Unfortunately, the commit that introduced that assert is just a one
> liner from Itamar back in 2003 without any context of what problem lead
> him to introduce the change.

I think it was introduced to catch some common bad usage patterns ...
like yours :)

If you want to chain the deferreds, use the dedicated helper
https://twistedmatrix.com/documents/current/core/howto/defer.html#chaining-deferreds

Deferred are not always 100% resolved/called.
You might have a deferred called, but the current result might be
another deferred... so it has no final result yet.

----

so in your case, instead of `returnValue(result)`  use

result = yield result
returnValue(result)


in this way, the result is resolved :)

Hope it helps



--
Adi Roiban




More information about the Twisted-Python mailing list