[Twisted-Python] Consistent interfaces to asynchronous partially-available services using Deferreds and state machines (was Re: Another approach to allowing __init__ to work with Deferreds)

Terry Jones terry at jon.es
Thu May 14 04:01:05 EDT 2009

>>>>> "Phil" == Phil Christensen <phil at bubblehouse.org> writes:

Phil> I don't know if I agree with the need for such a feature (that is,
Phil> deferred __init__ usage), but it was a very interesting coding
Phil> challenge I wanted to take a whack at. I *think* I might have found a
Phil> solution, but I don't know if it falls under the heading of
Phil> "decorator abuse" ;-)

Hi Phil

I finally had time to look at your solution a bit (though I've not run
it). It does a couple of things I wouldn't have thought of, like putting
the dictionary onto the deferredInit function. A couple of comments,
supposing I understand your code properly:

 - One thing I had hoped to avoid was to slow the class methods down by
   having them always check the original deferred (or a flag) before taking
   action. My approach does this by moving them aside and then putting them
   back in place once the deferred fires. Your solution requires that every
   decorated method does several extra things before it gets going. That
   could be greatly reduced if you were to check self.initDeferred.called
   and simply call the original function if the deferred has fired.

 - If multiple calls are made to instance methods before the init deferred
   has fired, they will, as I read it, all try to del
   deferredInit.waiting[self] in _finish. So I guess that del needs to be
   conditional or in a try/except.

 - Using self as a key into the dict on initDeferred seems like it
   addresses Glyph's observation/criticism that my approach raises
   questions wrt inheritance.

 - You could use chainDeferred where you're currently using
   .addCallbacks(resultDeferred.callback, resultDeferred.errback)

That's all for now. I'll see if I have more time to think about all this.
When I tried to use a decorator the first time, I was also using a super
class (whereas you're putting state into a dict on the deferredInit
function) but I got into a mess accessing self properly (partly because, I
think, I wanted to have a mixin class and I was looking at

In any case, thanks for replying, for playing with it, and for posting your
code. I got to learn new things as a result, which is really great :-)


More information about the Twisted-Python mailing list