问题
It's often useful to do this in standalone Python programs:
def excepthook(typ, value, tb):
traceback.print_exception(typ, value, tb)
pdb.pm()
...
if log.getEffectiveLevel() == logging.DEBUG:
sys.excepthook = excepthook
(i.e. if uncatched exception happens, program is dropped into pdb
post-mortem mode)
But that doesn't work with Tornado ioloop bc it seems that ioloop catches uncatched exceptions and prints or logs them. If I install above hook, the program enters post-mortem mode only on pressing Ctrl-C which is kind of late. :-)
Is there a way of making this happen without monkey-patching Tornado?
回答1:
You could subclass tornado's IOLoop and override handle_callback_exception. See http://www.tornadoweb.org/en/stable/ioloop.html#tornado.ioloop.IOLoop.handle_callback_exception
I guess this isn't much different from monkey-patching, though...
来源:https://stackoverflow.com/questions/20660657/start-pdb-on-exception-in-tornados-ioloop