From cf684a625273d08b46a274706a3f70171ebf1749 Mon Sep 17 00:00:00 2001 From: bd Date: Mon, 16 Jun 2025 16:48:23 -0400 Subject: Do not return the currently running task while collecting frames --- aergia | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/aergia b/aergia index af5a94e..f94781c 100755 --- a/aergia +++ b/aergia @@ -111,14 +111,14 @@ class Aergia(object): @staticmethod def print_samples(): - '''Pretty-print profiling information.''' + '''Pretty-print profiling results.''' if Aergia.total_samples > 0: - print("Results") print("FILE\tFUNC\tPERC\tACTUAL+CALCULATED=SECONDS") for key in Aergia.sort_samples(Aergia.samples): Aergia.print_sample(key) else: - print("No samples were gathered. This is likely a bug.") + print("No samples were gathered. If you are using concurrency, " + "this is likely a bug and you may run the profiler again.") @staticmethod def print_sample(key): @@ -157,7 +157,7 @@ class Aergia(object): '''Collects all stack frames that Aergia actually processes.''' frames = [] if Aergia.is_event_loop_running(): - frames = [task.get_coro().cr_frame for task in asyncio.all_tasks()] + frames = Aergia.get_current_idle_tasks() # Process all the frames to remove ones we aren't going to track. new_frames = [] @@ -186,6 +186,16 @@ class Aergia(object): new_frames.append(frame) return new_frames + @staticmethod + def get_current_idle_tasks(): + '''Obtains the stack of frames of all currently idle tasks.''' + curr_task = asyncio.current_task() + return [ + task.get_coro().cr_frame + for task in asyncio.all_tasks() + if task is not curr_task + ] + @staticmethod def frame_to_string(frame): '''Pretty-prints a frame as a function/file name and a line number. -- cgit v1.2.3