[Twisted-Python] How to chain deferred calls

hi, how can I chain in defer methods that could raise exception while showing the exception that coming from method? 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") ? t = Test() def abc1(self): if t.test() is None: raise Exception("Error11") else: return 1 def abc2(self): if t.test() is None: raise Exception("Error12") else: return 1 def abc3(self): if t.test() is None: raise Exception("Error13") else: return 1 Appreciate the help.

Add another .addErrback() directly after adc1() - to handle that specific exception... Kind regards, Valeriy Pogrebitskiy vpogrebi@verizon.net On Oct 21, 2009, at 4:12 PM, vitaly@synapticvision.com wrote:
hi, how can I chain in defer methods that could raise exception while showing the exception that coming from method? 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") ?
t = Test() def abc1(self): if t.test() is None: raise Exception("Error11") else: return 1
def abc2(self): if t.test() is None: raise Exception("Error12") else: return 1
def abc3(self): if t.test() is None: raise Exception("Error13") else: return 1
Appreciate the help.
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

Thank you for response. 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. Quoting "Valeriy Pogrebitskiy" <vpogrebi@verizon.net>:
Add another .addErrback() directly after adc1() - to handle that specific exception...
Kind regards,
Valeriy Pogrebitskiy vpogrebi@verizon.net
On Oct 21, 2009, at 4:12 PM, vitaly@synapticvision.com wrote:
hi, how can I chain in defer methods that could raise exception while showing the exception that coming from method? 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") ?
t = Test() def abc1(self): if t.test() is None: raise Exception("Error11") else: return 1
def abc2(self): if t.test() is None: raise Exception("Error12") else: return 1
def abc3(self): if t.test() is None: raise Exception("Error13") else: return 1
Appreciate the help.
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

I am confused... If an exception is being raised in adc1() - errback is supposed to be called and passed your Exception instance (wrapped up as an instance of twisted.python.failure.Failure). Kind regards, Valeriy Pogrebitskiy Email: vpogrebi@iname.com -----Original Message----- From: twisted-python-bounces@twistedmatrix.com [mailto:twisted-python-bounces@twistedmatrix.com] On Behalf Of vitaly@synapticvision.com Sent: Thursday, October 22, 2009 2:02 PM To: twisted-python@twistedmatrix.com Subject: Re: [Twisted-Python] How to chain deferred calls Thank you for response. 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. Quoting "Valeriy Pogrebitskiy" <vpogrebi@verizon.net>:
Add another .addErrback() directly after adc1() - to handle that specific exception...
Kind regards,
Valeriy Pogrebitskiy vpogrebi@verizon.net
On Oct 21, 2009, at 4:12 PM, vitaly@synapticvision.com wrote:
hi, how can I chain in defer methods that could raise exception while showing the exception that coming from method? 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") ?
t = Test() def abc1(self): if t.test() is None: raise Exception("Error11") else: return 1
def abc2(self): if t.test() is None: raise Exception("Error12") else: return 1
def abc3(self): if t.test() is None: raise Exception("Error13") else: return 1
Appreciate the help.
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

me too, may be its because of the way I'm calling the chain: return ( self.abc1(). addErrback(self.handleFailure1). addCallback(self.abc2,args). addCallback(self.abc3). addErrback(self.handleFailure2) ) I see no call to self.handleFailure1() at all, abc1() just fail on exception (if it was raised). Quoting "Valeriy Pogrebitskiy" <vpogrebi@verizon.net>:
I am confused... If an exception is being raised in adc1() - errback is supposed to be called and passed your Exception instance (wrapped up as an instance of twisted.python.failure.Failure).
Kind regards,
Valeriy Pogrebitskiy Email: vpogrebi@iname.com
-----Original Message----- From: twisted-python-bounces@twistedmatrix.com [mailto:twisted-python-bounces@twistedmatrix.com] On Behalf Of vitaly@synapticvision.com Sent: Thursday, October 22, 2009 2:02 PM To: twisted-python@twistedmatrix.com Subject: Re: [Twisted-Python] How to chain deferred calls
Thank you for response.
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.
Quoting "Valeriy Pogrebitskiy" <vpogrebi@verizon.net>:
Add another .addErrback() directly after adc1() - to handle that specific exception...
Kind regards,
Valeriy Pogrebitskiy vpogrebi@verizon.net
On Oct 21, 2009, at 4:12 PM, vitaly@synapticvision.com wrote:
hi, how can I chain in defer methods that could raise exception while showing the exception that coming from method? 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") ?
t = Test() def abc1(self): if t.test() is None: raise Exception("Error11") else: return 1
def abc2(self): if t.test() is None: raise Exception("Error12") else: return 1
def abc3(self): if t.test() is None: raise Exception("Error13") else: return 1
Appreciate the help.
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

After taking closer look at your code - this is obvious... To use callbacks - you MUST use deferred. In your case - each method in a chain (except the last one) must return deferred. Without that - you can't construct such chain. Your abc1() and abc2() must return deferred (or be decorated by a method that returns deferred)... Kind regards, Valeriy Pogrebitskiy Email: vpogrebi@iname.com -----Original Message----- From: twisted-python-bounces@twistedmatrix.com [mailto:twisted-python-bounces@twistedmatrix.com] On Behalf Of vitaly@synapticvision.com Sent: Thursday, October 22, 2009 6:38 PM To: twisted-python@twistedmatrix.com Subject: Re: [Twisted-Python] How to chain deferred calls me too, may be its because of the way I'm calling the chain: return ( self.abc1(). addErrback(self.handleFailure1). addCallback(self.abc2,args). addCallback(self.abc3). addErrback(self.handleFailure2) ) I see no call to self.handleFailure1() at all, abc1() just fail on exception (if it was raised). Quoting "Valeriy Pogrebitskiy" <vpogrebi@verizon.net>:
I am confused... If an exception is being raised in adc1() - errback is supposed to be called and passed your Exception instance (wrapped up as an instance of twisted.python.failure.Failure).
Kind regards,
Valeriy Pogrebitskiy Email: vpogrebi@iname.com
-----Original Message----- From: twisted-python-bounces@twistedmatrix.com [mailto:twisted-python-bounces@twistedmatrix.com] On Behalf Of vitaly@synapticvision.com Sent: Thursday, October 22, 2009 2:02 PM To: twisted-python@twistedmatrix.com Subject: Re: [Twisted-Python] How to chain deferred calls
Thank you for response.
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.
Quoting "Valeriy Pogrebitskiy" <vpogrebi@verizon.net>:
Add another .addErrback() directly after adc1() - to handle that specific exception...
Kind regards,
Valeriy Pogrebitskiy vpogrebi@verizon.net
On Oct 21, 2009, at 4:12 PM, vitaly@synapticvision.com wrote:
hi, how can I chain in defer methods that could raise exception while showing the exception that coming from method? 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") ?
t = Test() def abc1(self): if t.test() is None: raise Exception("Error11") else: return 1
def abc2(self): if t.test() is None: raise Exception("Error12") else: return 1
def abc3(self): if t.test() is None: raise Exception("Error13") else: return 1
Appreciate the help.
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

Correct, sorry, tried: return ( self.abc1(). addErrback(self.handleFailure1). addCallback(self.abc2,args). addCallback(self.abc3). addErrback(self.handleFailure2) ) def abc1(self): if t.test() is None: raise Exception("Error11") else: d = defer.Deferred() d.callback(1) return d Still in case of exception from abc1() - self.handleFailure1() never called, instead of it abc1() just fail. Do I need "raise Exception("Error11")" also return as deferred ? Than how? Quoting "Valeriy Pogrebitskiy" <vpogrebi@verizon.net>:
After taking closer look at your code - this is obvious...
To use callbacks - you MUST use deferred. In your case - each method in a chain (except the last one) must return deferred. Without that - you can't construct such chain.
Your abc1() and abc2() must return deferred (or be decorated by a method that returns deferred)...
Kind regards,
Valeriy Pogrebitskiy Email: vpogrebi@iname.com
-----Original Message----- From: twisted-python-bounces@twistedmatrix.com [mailto:twisted-python-bounces@twistedmatrix.com] On Behalf Of vitaly@synapticvision.com Sent: Thursday, October 22, 2009 6:38 PM To: twisted-python@twistedmatrix.com Subject: Re: [Twisted-Python] How to chain deferred calls
me too, may be its because of the way I'm calling the chain:
return ( self.abc1(). addErrback(self.handleFailure1). addCallback(self.abc2,args). addCallback(self.abc3). addErrback(self.handleFailure2) )
I see no call to self.handleFailure1() at all, abc1() just fail on exception (if it was raised).
Quoting "Valeriy Pogrebitskiy" <vpogrebi@verizon.net>:
I am confused... If an exception is being raised in adc1() - errback is supposed to be called and passed your Exception instance (wrapped up as an instance of twisted.python.failure.Failure).
Kind regards,
Valeriy Pogrebitskiy Email: vpogrebi@iname.com
-----Original Message----- From: twisted-python-bounces@twistedmatrix.com [mailto:twisted-python-bounces@twistedmatrix.com] On Behalf Of vitaly@synapticvision.com Sent: Thursday, October 22, 2009 2:02 PM To: twisted-python@twistedmatrix.com Subject: Re: [Twisted-Python] How to chain deferred calls
Thank you for response.
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.
Quoting "Valeriy Pogrebitskiy" <vpogrebi@verizon.net>:
Add another .addErrback() directly after adc1() - to handle that specific exception...
Kind regards,
Valeriy Pogrebitskiy vpogrebi@verizon.net
On Oct 21, 2009, at 4:12 PM, vitaly@synapticvision.com wrote:
hi, how can I chain in defer methods that could raise exception while showing the exception that coming from method? 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") ?
t = Test() def abc1(self): if t.test() is None: raise Exception("Error11") else: return 1
def abc2(self): if t.test() is None: raise Exception("Error12") else: return 1
def abc3(self): if t.test() is None: raise Exception("Error13") else: return 1
Appreciate the help.
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

Hello,
On Oct 21, 2009, at 4:12 PM, vitaly@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 (http://twistedmatrix.com/projects/core/documentation/howto/async.html) 2. Deferred Reference (http://twistedmatrix.com/projects/core/documentation/howto/defer.html) 3. Generating Deferreds (http://twistedmatrix.com/projects/core/documentation/howto/gendefer.html) Specifically, the most relevant is the Visual Explanation: http://twistedmatrix.com/projects/core/documentation/howto/defer.html#auto2 This should give you the info you need, but I'll explain a little more anyway.
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

Thank you a lot for pointing me to the Twisted doc-s, but we're discussing the following snippet: return ( self.abc1(). addErrback(self.handleFailure1). addCallback(self.abc2,args). addCallback(self.abc3). addErrback(self.handleFailure2) ) def abc1(self): if t.test() is None: raise Exception("Error11") else: d = defer.Deferred() d.callback(1) return d and basically, I've expected in case of exception self.handleFailure1() to be called, but I don't see it happen. cheers. Quoting "Paul Goins" <general@vultaire.net>:
Hello,
On Oct 21, 2009, at 4:12 PM, vitaly@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 (http://twistedmatrix.com/projects/core/documentation/howto/async.html) 2. Deferred Reference (http://twistedmatrix.com/projects/core/documentation/howto/defer.html) 3. Generating Deferreds (http://twistedmatrix.com/projects/core/documentation/howto/gendefer.html)
Specifically, the most relevant is the Visual Explanation: http://twistedmatrix.com/projects/core/documentation/howto/defer.html#auto2 This should give you the info you need, but I'll explain a little more anyway.
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
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

vitaly@synapticvision.com wrote: [...]
def abc1(self): if t.test() is None: raise Exception("Error11") else: d = defer.Deferred() d.callback(1) return d
and basically, I've expected in case of exception self.handleFailure1() to be called, but I don't see it happen.
This is a function that either returns a Deferred or raises an exception. This isn't a Twisted issue, it's simply a Python one: in “func1().func2()”, if func1 raises an exception then func2 will not be invoked. That's fundamental to what raising an exception in Python means. If you want to convert this to something that always returns a Deferred, you can either rewrite it, e.g. using “return defer.fail(Exception('Error11'))”, or use maybeDeferred which will intercept exceptions for you, e.g.: return ( maybeDeferred(self.abc1). addErrback(self.handleFailure1). # etc... ) You can find maybeDeferred in the twisted.internet.defer module. -Andrew.

This is it! Thank you. Quoting "Andrew Bennetts" <andrew@bemusement.org>:
vitaly@synapticvision.com wrote: [...]
def abc1(self): if t.test() is None: raise Exception("Error11") else: d = defer.Deferred() d.callback(1) return d
and basically, I've expected in case of exception self.handleFailure1() to be called, but I don't see it happen.
This is a function that either returns a Deferred or raises an exception. This isn't a Twisted issue, it's simply a Python one: in “func1().func2()”, if func1 raises an exception then func2 will not be invoked. That's fundamental to what raising an exception in Python means.
If you want to convert this to something that always returns a Deferred, you can either rewrite it, e.g. using “return defer.fail(Exception('Error11'))”, or use maybeDeferred which will intercept exceptions for you, e.g.:
return ( maybeDeferred(self.abc1). addErrback(self.handleFailure1). # etc... )
You can find maybeDeferred in the twisted.internet.defer module.
-Andrew.
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (4)
-
Andrew Bennetts
-
Paul Goins
-
Valeriy Pogrebitskiy
-
vitaly@synapticvision.com