def get_task_info(task: asyncio.Task) -> Dict: def _format_frame(f): keys = ["f_code", "f_lineno"] return OrderedDict([(k, str(getattr(f, k))) for k in keys]) info = OrderedDict( txt=str(task), type=str(type(task)), done=task.done(), cancelled=False, stack=None, exception=None, ) if not task.done(): info["stack"] = [_format_frame(x) for x in task.get_stack()] else: if task.cancelled(): info["cancelled"] = True else: # WARNING: raise if not done or cancelled exc = task.exception() info["exception"] = f"{type(exc)}: {str(exc)}" if exc else None return info
def catch_task_exception(task: asyncio.Task): exc = task.exception() if not exc: return False _logger.exception('Task exception: %r', exc, stack_info=task.get_stack()) return True