[Twisted-Python] Understanding deferred and error handling

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Fri Apr 22 05:53:58 MDT 2011


On 12:53 am, david at silveregg.co.jp wrote:
>On 04/21/2011 10:12 AM, exarkun at twistedmatrix.com wrote:
>>log.err takes an additional argument which you can use easily like 
>>this:
>>
>>      def main():
>>          d = remote_call()
>>          d.addErrback(log.err, "remote_call failed")
>>          def _stop(ignored):
>>              reactor.stop()
>>          d.addCallback(_stop)
>>
>>The message you pass here will replace the "Unhandled error in 
>>Deferred"
>>text in the report of the failure.
>
>ok, thanks for the tip.
>>
>>Another option is to enable Deferred debugging, which I think someone
>>mentioned earlier in the thread:
>>
>>      from twisted.internet.defer import setDebugging
>>      setDebugging(True)
>>
>>or
>>
>>      twistd --debug ...
>>
>>or
>>
>>      trial --debug ...
>>
>>This causes Deferred to capture the stack at the time it is created 
>>and
>>the time it is called back.  The "Unhandled error" case will log this
>>information.  This should give you the traceback you want.  This is 
>>not
>>enabled by default because (I suppose) capturing stacks is very
>>expensive.
>
>I am not so interested in "debug runs", but more in a way to get more
>informations of our tool stack bugs when run in production. I guess 
>what
>I am looking for is some good practices for dealing with errors, then.
>Maybe a piece of code in twisted itself ?

It's mostly just a matter of having errbacks in all the right places. 
Don't let a Deferred get garbage collected with a Failure.  Always have 
an error handler at the end, even if it's just an error handler to log 
the failure.

Jean-Paul




More information about the Twisted-Python mailing list