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

Chris Withers chris at withers.org
Tue Feb 19 06:34:59 MST 2019


On 19/02/2019 11:41, Adi Roiban wrote:
>
> I think it was introduced to catch some common bad usage patterns ...
> like yours :)

Not a massively helpful comment.

> 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 :)

The methods being hooked don't necessarily return deferreds.
I'd like it to be an explicit choice of the caller, ie:

result = yield SomeProtocol.onMessage.called()
# okay, we got here, we know onMessage was called,
# now we might want to tick a clock, or otherwise simulate
# async state manipulation.
# now I want to make sure the deferred chain on the onMessage result has 
been completed:
yield result

cheers,

Chris




More information about the Twisted-Python mailing list