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

Phil Mayers p.mayers at imperial.ac.uk
Thu Sep 30 07:03:01 MDT 2010


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.
> Most prominent is reactor.callLater.

One quick thing you might be able to do is use t.i.task.deferLater 
instead of reactor.callLater, like so:

def doStuff():
   breakobj = Break()
   d = task.deferLater(reactor, 2, breakobj)
   return d

task.deferLater will schedule the call, but runs the call in a context 
which means any exceptions raised flow down the deferred errback. This:

@inlineCallbacks
def loop():
   try:
     yield doStuff()
   except Exception, e:
     log.err(e)

...will then work, and "see" the exception.




More information about the Twisted-Python mailing list