Skip to content

Commit 977c15a

Browse files
authored
Merge pull request #103 from mgmacias95/pythonGH-91048-tasks
chore: Refactor
2 parents 6f8aa6b + 8d566c6 commit 977c15a

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

Lib/asyncio/tools.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Tool to analyze tasks running in a asyncio script."""
2+
13
from dataclasses import dataclass
24
from collections import defaultdict
35
from 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 ───────────────────────
7068
def _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

143137
def 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

Comments
 (0)