From c77f7674bd6e68e03bc8f1f14ab0789f66af42d5 Mon Sep 17 00:00:00 2001 From: bd Date: Mon, 11 Aug 2025 23:09:48 -0400 Subject: Do not profile frames if they belong to an event loop without a task. --- aergia/aergia.py | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/aergia/aergia.py b/aergia/aergia.py index a41ef92..fcff1c7 100755 --- a/aergia/aergia.py +++ b/aergia/aergia.py @@ -275,12 +275,11 @@ class Aergia(object): included elsewhere.''' idle = [] - # set this when we start processing a loop. - # it is required later, but I only want to set it once. + # required later Aergia.current_task = asyncio.current_task(loop) for task in asyncio.all_tasks(loop): - if not Aergia._should_trace_task(task): + if not Aergia._task_is_valid(task) or task == Aergia.current_task: continue coro = task.get_coro() @@ -317,41 +316,36 @@ class Aergia(object): if curr is not None: tasks = getattr(curr, '_children', []) if any( - Aergia._should_trace_task(task) + Aergia._task_is_valid(task) for task in tasks ): return None - return deepest_frame @staticmethod - def _should_trace_task(task): + def _task_is_valid(task): '''Returns FALSE if TASK is uninteresting to the user. - A task is interesting if it is not CURRENT_TASK, if it has actually - started executing, and if a child task did not originate from it. + A task is interesting if it has actually started executing, and if + a child task did not originate from it. ''' if not isinstance(task, asyncio.Task): return False - # the task is not idle - if task == Aergia.current_task: + if task._state != 'PENDING': return False coro = task.get_coro() - # the task hasn't even run yet - # assumes that all started tasks are sitting at an await - # statement. - # if this isn't the case, the associated coroutine will - # be 'waiting' on the coroutine declaration. No! Bad! + # the task is not running and has not started running. + # if this is the case, the associated coroutine will be 'waiting' + # on the coroutine declaration. No! Bad! frame, awaitable = Aergia._trace_down(coro) - if frame is None or awaitable is None: + if task != Aergia.current_task and \ + (frame is None or awaitable is None): return False - frame = getattr(coro, 'cr_frame', None) - - return Aergia._should_trace(frame.f_code.co_filename) + return True @staticmethod def _trace_down(awaitable) -> \ -- cgit v1.2.3