1+ """Tool to analyze tasks running in a asyncio script."""
2+
13from dataclasses import dataclass
24from collections import defaultdict
35from itertools import count
@@ -46,10 +48,6 @@ def _cor_node(parent_key, frame_name):
4648 bucket [frame_name ] = node_key
4749 return node_key
4850
49- # touch every task so it’s present even if it awaits nobody
50- for tid in id2name :
51- children [(NodeType .TASK , tid )]
52-
5351 # lay down parent ➜ …frames… ➜ child paths
5452 for parent_id , stack , child_id in awaits :
5553 cur = (NodeType .TASK , parent_id )
@@ -69,7 +67,6 @@ def _roots(id2label, children):
6967# ─── detect cycles in the task-to-task graph ───────────────────────
7068def _task_graph (awaits ):
7169 """Return {parent_task_id: {child_task_id, …}, …}."""
72- from collections import defaultdict
7370 g = defaultdict (set )
7471 for parent_id , _stack , child_id in awaits :
7572 g [parent_id ].add (child_id )
@@ -106,7 +103,7 @@ def dfs(v):
106103
107104
108105# ─── PRINT TREE FUNCTION ───────────────────────────────────────
109- def build_async_tree (result , task_emoji = "(T)" , cor_emoji = "" , printer = print ):
106+ def build_async_tree (result , task_emoji = "(T)" , cor_emoji = "" ):
110107 """
111108 Build a list of strings for pretty-print a async call tree.
112109
@@ -134,10 +131,7 @@ def render(node, prefix="", last=True, buf=None):
134131 render (kid , new_pref , i == len (kids ) - 1 , buf )
135132 return buf
136133
137- result = []
138- for r , root in enumerate (_roots (labels , children )):
139- result .append (render (root ))
140- return result
134+ return [render (root ) for root in _roots (labels , children )]
141135
142136
143137def build_task_table (result ):
@@ -209,7 +203,7 @@ def display_awaited_by_tasks_tree(pid: int) -> None:
209203
210204 tasks = _get_awaited_by_tasks (pid )
211205 try :
212- result = print_async_tree (tasks )
206+ result = build_async_tree (tasks )
213207 except CycleFoundException as e :
214208 _print_cycle_exception (e )
215209 sys .exit (1 )
0 commit comments