[Twisted-Python] Inline callbacks: generating the same Deferred at multiple places

Pierre Jaury pierre at jaury.eu
Fri Feb 22 10:40:05 EST 2013

Hey everyone,

First of all, I am a french developer mostly dealing with security
matters, I have been a Twisted enthusiast for a couple of years now and
have been hacking around with Twisted for the very last couple of months.

I am aware that Deferred-related classes are very stable nowadays and
that such a change does not stand half a chance to get anywhere. Anyway,
Twisted behavior kind of annoys me when it comes to inline callback

Indeed, the gotResult function used when generating Deferred objects
from a function decorated with inlineCallbacks currently looks like:

            def gotResult(r):
                if waiting[0]:
                    waiting[0] = False
                    waiting[1] = r
                    _inlineCallbacks(r, g, deferred)

So far the function does not return anything explicitely, thus following
callbacks get a very nice `None` as first argument. This is not so
annoying when using inline callbacks with very simple and
straightforward cases as advised by Twisted documentation. Anyway, when
generating the same Deferred object from multiple
inlineCallbacks-decorated functions, one would expect every `yield`
statement to return the same value.

As a matter of fact, this is not the case (the first generation of the
Deferred object will return its actual current state but later
generations will return `None`). Because a Deferred object generation
from an inlineCallbacks-decorated function does not allow for anything
but waiting for the Deferred object to fire and returning its state, it
would probably not harm to forward this state to the next callback as
well. Something like:

            def gotResult(r):
                if waiting[0]:
                    waiting[0] = False
                    waiting[1] = r
                    _inlineCallbacks(r, g, deferred)
                return r

would probably do the trick and allow for complex scenarios where the
same Deferred object is generated multiple times by different functions
before being fired.

As I am not an accomplished Twisted hacker, I cannot say if this kind of
change would harm or break any code out there. Yet it seems to me that
according to inlineCallbacks behavior, it is very unlikely to have any
consequence at all but improving (extending at least) inlineCallbacks

Am I so wrong? (I would very much like someone to point out the one huge
detail I completely forgot before I make this change by monkey patching
and submit a feature request :)


Pierre Jaury <pierre at jaury.eu>
Weblog - http://kaiyou.fr
GPG ID - E804FB60

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: OpenPGP digital signature
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20130222/2e9aed4c/attachment.pgp 

More information about the Twisted-Python mailing list