def abort_on_exception(func): # noqa
"""
This function decorator wraps the run() method of a thread
so that any exceptions in that thread will be logged and
cause the threads 'abort' signal to be emitted with the exception
as an argument. This way all exception handling can occur
on the main thread.
Note that the entire sys.exc_info() tuple is passed out, this
allows the current traceback to be used in the other thread.
"""
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
thread_object = args[0]
exc_type, exc_value, exc_tb = exc_info = sys.exc_info()
filename, line_num, func_name, text = traceback.extract_tb(exc_tb)[-1]
logger.error('Exception Thrown from [%s] on line [%s] via function [%s]' % (filename, line_num, func_name))
logger.error('Exception type %s: %s' % (e.__class__.__name__, e.message))
thread_object.emit('aborted', exc_info)
return wrapper
评论列表
文章目录