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

Chris Withers chris at simplistix.co.uk
Tue Sep 28 09:48:02 EDT 2010


Hi All,

The attached .py file demonstrates a problem I'm having with a little 
scheduler I need to maintain.

The loop() function is supposed to be a "catch all and report" error 
handler. However, when the async code initiated by doStuff throws the 
AttributeError, what actually gets logged is:

2010-09-28 14:41:15,706 ERROR   : log         (14757|7f691ba676e0): 
Unhandled Error
Traceback (most recent call last):
   File "test_looping.py", line 41, in <module>
     reactor.run()
   File "Twisted-8.2.0-py2.5-linux-x86_64.egg/twisted/internet/base.py", 
line 1128, in run
     self.mainLoop()
   File "Twisted-8.2.0-py2.5-linux-x86_64.egg/twisted/internet/base.py", 
line 1137, in mainLoop
     self.runUntilCurrent()
--- <exception caught here> ---
   File "Twisted-8.2.0-py2.5-linux-x86_64.egg/twisted/internet/base.py", 
line 757, in runUntilCurrent
     call.func(*call.args, **call.kw)
   File "test_looping.py", line 24, in __call__
     del self.connector
exceptions.AttributeError: Break instance has no attribute 'connector'

2010-09-28 14:41:15,707 ERROR   : log         (14757|7f691ba676e0): 
Unhandled scheduled exception
Traceback (most recent call last):
   File "test_looping.py", line 41, in <module>
     reactor.run()
   File "Twisted-8.2.0-py2.5-linux-x86_64.egg/twisted/internet/base.py", 
line 1128, in run
     self.mainLoop()
   File "Twisted-8.2.0-py2.5-linux-x86_64.egg/twisted/internet/base.py", 
line 1137, in mainLoop
     self.runUntilCurrent()
--- <exception caught here> ---
   File "test_looping.py", line 35, in loop
     yield doStuff()
exceptions.GeneratorExit:

But, more crucially, the looping call then appears to stop.
What can I do to get the exception logged and then everything handled 
sanely such that the looping call can continue and my lopp function will 
keep getting called once every second rather than stopping?

cheers,

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


More information about the Twisted-Python mailing list