I have an asynchronous piece of code that executed concurrently:
@tornado.web.asynchronous def execute(self, func, *args): def callback(future): try: to_return = json.dumps(future.result()) self.write(to_return) self.flush() self.finish() except: error = error = self.get_exception() self.set_status(500) self.finish(json.dumps({"error": error)) EXECUTOR.submit( partial(func, *args) ).add_done_callback( lambda future: tornado.ioloop.IOLoop.instance().add_callback( partial(callback, future))) def get_exception(self): exc_type, exc_obj, tb = sys.exc_info() f = tb.tb_frame lineno = tb.tb_lineno filename = f.f_code.co_filename linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) return {'error_msg': 'LINE {} "{}"): {}, {}'.format(filename, lineno, line.strip(), exc_obj, exc_obj)} This works well, except if an execution is thrown somewhere in the function, the stack trace only goes back to the point where it was thrown in the callback (ie where the future was executed), not the where it actually happened in the code.
Is it possible to capture the exception in the actually function where it was originally thrown?