[Twisted-Python] How to chain deferred calls

Paul Goins general at vultaire.net
Thu Oct 22 19:41:56 EDT 2009


>>> On Oct 21, 2009, at 4:12 PM, vitaly at synapticvision.com wrote:
>>>> hi,
>>>> how can I chain in defer methods that could raise exception while
>>>> showing the exception that coming from method?

This is covered in the Twisted documentation.  There's 3 pages I often
have reviewed when confused about something:

1. Asynchronous Programming with Twisted
2. Deferred Reference
3. Generating Deferreds

Specifically, the most relevant is the Visual Explanation:
This should give you the info you need, but I'll explain a little more

>>>> I mean, if I'll do:
>>>> return (
>>>>   adc1().abc2().abc3().addErrback("Common2AllException")
>>>> )
>>>> than "Common2AllException" will be raised if abc1() raise Exception,
>>>> but I'd like to see the adc1() own exception (and not
>>>> "Common2AllException") ?

If abc1 raises an exception, it's going to go through each step of the
callback/errback chain until it finds an errback.  If the only thing you
have is attached at the very end, then that's where the exception will
be handled.

> the issue is if adc1() will raise up an exception, than following it
> .addErrback() will never be called cause adc1() will exit or raise
> Exception.

Let's say that your abc() was returning a Deferred.  In this case, doing
abc().addErrback("Common2AllException") would make sense.  However, your
example either returns 1 (not a Deferred), or it raises an Exception.
Neither one will chain like this.  The example is bad.

If you can provide a better example, please do.  In any case, please
review the documentation, especially the "visual explanation" I
mentioned above.  And finally, do a search on the web for "twisted
inlineCallbacks decorator" - that makes writing a lot of Deferred-using
code much easier, and helped me out a lot as a beginner.

- Paul

More information about the Twisted-Python mailing list