[Twisted-Python] Understanding deferred and error handling

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Wed Apr 20 21:12:43 EDT 2011


On 01:04 am, cournape at gmail.com wrote:
>but not what I consider the meat of my issue, that is
>the lack of traceback. Fixing and simplifying my initial example:
>
>import twisted.web.client
>
>from twisted.internet import defer
>from twisted.internet import reactor
>
>def remote_call():
>    d = twisted.web.client.getPage("http://localhost:8083")
>    return d
>
>def main():
>    d = remote_call()
>    def _stop(arg):
>        reactor.stop()
>        return arg
>    d.addBoth(_stop)
>
>reactor.callWhenRunning(main)
>reactor.run()
>
>This will correctly signal an error, but the output:
>
>Unhandled error in Deferred:
>Unhandled Error
>Traceback (most recent call last):
>Failure: twisted.internet.error.ConnectionRefusedError: Connection was
>refused by other side: 61: Connection refused.
>
>is not informative. I was hoping for a way to know that the error
>happens inside remote_call through e.g. an errback in main. If this is
>not possible, how do people generally handle those issues in
>asynchronous code ?

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.

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.

Jean-Paul



More information about the Twisted-Python mailing list