[Twisted-Python] How to chain deferred calls

vitaly at synapticvision.com vitaly at synapticvision.com
Thu Oct 22 21:49:33 EDT 2009


Very, very close to want I wanted to understand, thank you!

One more q please: what if I complicate the abc1() like following, is  
this expected to work or again I've missed the point?

   return (
          self.abc1().
          addErrback(self.handleFailure1).
          addCallback(self.abc2,args).
          addCallback(self.abc3).
          addErrback(self.handleFailure2)
     )

     def abc1(self):

         d = defer.Deferred()

         c = myClass()
         result = c.myTestFunc()
         if result != 1:
             d.addCallback(raiser)

         d.callback(result)
         return d

     def raiser(self, _):
         raise Exception("Error11")



Quoting "Terry Jones" <terry at jon.es>:

> If your code looked like this (pseudocode), you would see the exception
>
>     return (
>          self.abc1().
>          addErrback(self.handleFailure1).
>          addCallback(self.abc2,args).
>          addCallback(self.abc3).
>          addErrback(self.handleFailure2)
>     )
>
>     def abc1(self):
>         d = defer.Deferred()
>         d.addCallback(raiser)
>         d.callback(1)
>         return d
>
>     def raiser(self, _):
>         raise Exception("Error11")
>
> Because the thing that raises is being called by Twisted's deferred class,
> and its exception is caught and routed to d's errback chain and winds up in
> the handleFailure1 method.
>
> Does that make sense?
>
> Terry
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>





More information about the Twisted-Python mailing list