1
+ """Tool to analyze tasks running in a asyncio script."""
2
+
1
3
from dataclasses import dataclass
2
4
from collections import defaultdict
3
5
from itertools import count
@@ -46,10 +48,6 @@ def _cor_node(parent_key, frame_name):
46
48
bucket [frame_name ] = node_key
47
49
return node_key
48
50
49
- # touch every task so it’s present even if it awaits nobody
50
- for tid in id2name :
51
- children [(NodeType .TASK , tid )]
52
-
53
51
# lay down parent ➜ …frames… ➜ child paths
54
52
for parent_id , stack , child_id in awaits :
55
53
cur = (NodeType .TASK , parent_id )
@@ -69,7 +67,6 @@ def _roots(id2label, children):
69
67
# ─── detect cycles in the task-to-task graph ───────────────────────
70
68
def _task_graph (awaits ):
71
69
"""Return {parent_task_id: {child_task_id, …}, …}."""
72
- from collections import defaultdict
73
70
g = defaultdict (set )
74
71
for parent_id , _stack , child_id in awaits :
75
72
g [parent_id ].add (child_id )
@@ -106,7 +103,7 @@ def dfs(v):
106
103
107
104
108
105
# ─── 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 = "" ):
110
107
"""
111
108
Build a list of strings for pretty-print a async call tree.
112
109
@@ -134,10 +131,7 @@ def render(node, prefix="", last=True, buf=None):
134
131
render (kid , new_pref , i == len (kids ) - 1 , buf )
135
132
return buf
136
133
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 )]
141
135
142
136
143
137
def build_task_table (result ):
@@ -209,7 +203,7 @@ def display_awaited_by_tasks_tree(pid: int) -> None:
209
203
210
204
tasks = _get_awaited_by_tasks (pid )
211
205
try :
212
- result = print_async_tree (tasks )
206
+ result = build_async_tree (tasks )
213
207
except CycleFoundException as e :
214
208
_print_cycle_exception (e )
215
209
sys .exit (1 )
0 commit comments