[Twisted-Python] how to write a safe catch-all

Chris Withers chris at simplistix.co.uk
Thu Sep 30 09:36:56 EDT 2010


On 30/09/2010 14:03, Phil Mayers wrote:
> On 30/09/10 13:44, Phil Mayers wrote:
>
>> This is because (as you've discovered) there are circumstances when a
>> function is called in Twisted where the exception can't propagate "up"
>> to the caller, because the caller is no longer higher in the call stack.

Indeed, but, as I keep saying, I need to build a scheduler that's 
resilient to errors in the tasks its running ;-)

>> Most prominent is reactor.callLater.

...which I'm not actually using, it was just a small-as-possible way I 
could simulate the failure mode (rather than the specific failure) I 
need to protect against.

> @inlineCallbacks
> def loop():
>     try:
>       yield doStuff()
>     except Exception, e:
>       log.err(e)
>
> ...will then work, and "see" the exception.

Actually, what appears to work is simply changing `loop` to not be an 
async fuction:

def loop():
     try:
         doStuff()
     except Exception,e:
         log.err(None,'Unhandled scheduled exception')

looper = task.LoopingCall(loop)
looper.start(1.0)
reactor.run()

This appears to solve most of the problems:
- the schedule now keeps running regardless
- exceptions in doStuff and below get logged

However, it now has the problem that if doStuff *does* return a deferred 
and it errbacks, the I get the ugly:

2010-09-30 14:31:12,478 ERROR   : log         (22159|7fc6c5c356e0): 
Unhandled error in Deferred:
2010-09-30 14:31:12,479 ERROR   : log         (22159|7fc6c5c356e0): 
Unhandled Error
Traceback (most recent call last):
Failure: twisted.python.failure.DefaultException: Break!

...rather than having my exception handler log with a meaningful message.

I've attached the full example as I have it again...

Is there any way I can get both errbacks *and* exceptions handled nicely 
in my `loop` call?

cheers,

Chris
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: test_looping.py
Url: http://twistedmatrix.com/pipermail/twisted-python/attachments/20100930/d8bc6008/attachment-0001.txt 


More information about the Twisted-Python mailing list