[Twisted-Python] deferred proposed update

Fabrice Coudert fabrice.coudert at free.fr
Fri Jan 16 10:22:58 EST 2004


I 'd like to add a pre-processing and post-processing callback in a 
deferred callback list. I did somthing like this :

my_defer = getADeferredForSomething()
new_defer = defer.Deferred()
my_defer = new_defer

My problem is that postprocessing doesn't receive the result of the last 
callback in my_defer, but it reveive None.
This can be explained in twisted source by that chainDeferred(d) is 
equivalent to addCallbacks(d.callback, d.errback), but this two methods 
are not really sort of callback methods for deferred coz they didn't 
return a result.

So I patched Deferred class to add "return self.result", and it works well :

Deferred_old = defer.Deferred
class Deferred_new(Deferred_old):
    def callback(self, result):
        Deferred_old.callback(self, result)
        return self.result
    def errback(self, fail=None):
        Deferred_old.errback(self, fail)
        return self.result
defer.Deferred = Deferred_new

Notes by doing this you can also get final result when firing the 
callbacks :

final_result = my_defer.callback(result)

I dunno if this patch will fail something else in twisted.


More information about the Twisted-Python mailing list