Skip to content

Commit 4af64e6

Browse files
committed
Merge branch 'feature/pending-tasks-csv-bsv' into feature/pending-tasks-BANANAS
# Conflicts: # Lib/asyncio/__main__.py
2 parents 005e147 + 0d8d00a commit 4af64e6

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

Lib/asyncio/__main__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import argparse
22
import ast
33
import asyncio
4-
import asyncio.tools
54
import concurrent.futures
65
import contextvars
76
import inspect
@@ -11,6 +10,9 @@
1110
import threading
1211
import types
1312
import warnings
13+
from asyncio.tools import (TaskTableOutputFormat,
14+
display_awaited_by_tasks_table,
15+
display_awaited_by_tasks_tree)
1416

1517
from _colorize import get_theme
1618
from _pyrepl.console import InteractiveColoredConsole
@@ -153,7 +155,7 @@ def interrupt(self) -> None:
153155
"ps", help="Display a table of all pending tasks in a process"
154156
)
155157
ps.add_argument("pid", type=int, help="Process ID to inspect")
156-
formats = [fmt.value for fmt in asyncio.tools.TaskTableOutputFormat]
158+
formats = [fmt.value for fmt in TaskTableOutputFormat]
157159
big_secret = asyncio.tools.TaskTableOutputFormat.bsv.value
158160
formats_to_show = [
159161
fmt for fmt in formats if fmt != big_secret
@@ -168,10 +170,10 @@ def interrupt(self) -> None:
168170
args = parser.parse_args()
169171
match args.command:
170172
case "ps":
171-
asyncio.tools.display_awaited_by_tasks_table(args.pid, args.format)
173+
display_awaited_by_tasks_table(args.pid, format=args.format)
172174
sys.exit(0)
173175
case "pstree":
174-
asyncio.tools.display_awaited_by_tasks_tree(args.pid)
176+
display_awaited_by_tasks_tree(args.pid)
175177
sys.exit(0)
176178
case None:
177179
pass # continue to the interactive shell

Lib/asyncio/tools.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
"""Tools to analyze tasks running in asyncio programs."""
22

3-
from collections import defaultdict, namedtuple
3+
from collections import defaultdict
44
import csv
55
from itertools import count
66
from enum import Enum, StrEnum, auto
77
import sys
88
from _remote_debugging import RemoteUnwinder, FrameInfo
99

10-
1110
class NodeType(Enum):
1211
COROUTINE = 1
1312
TASK = 2
@@ -242,10 +241,7 @@ class TaskTableOutputFormat(StrEnum):
242241
# https://www.youtube.com/watch?v=RrsVi1P6n0w
243242

244243

245-
def display_awaited_by_tasks_table(
246-
pid: int,
247-
format: TaskTableOutputFormat | str = TaskTableOutputFormat.table
248-
) -> None:
244+
def display_awaited_by_tasks_table(pid, *, format=TaskTableOutputFormat.table):
249245
"""Build and print a table of all pending tasks under `pid`."""
250246

251247
tasks = _get_awaited_by_tasks(pid)
@@ -254,30 +250,38 @@ def display_awaited_by_tasks_table(
254250
if format == TaskTableOutputFormat.table:
255251
_display_awaited_by_tasks_table(table)
256252
else:
257-
_display_awaited_by_tasks_csv(table, format)
253+
_display_awaited_by_tasks_csv(table, format=format)
254+
255+
256+
_row_header = ('tid', 'task id', 'task name', 'coroutine stack',
257+
'awaiter chain', 'awaiter name', 'awaiter id')
258258

259259

260-
def _display_awaited_by_tasks_table(table) -> None:
261-
# Print the table in a simple tabular format
262-
print(
263-
f"{'tid':<10} {'task id':<20} {'task name':<20} {'coroutine stack':<50} {'awaiter chain':<50} {'awaiter name':<15} {'awaiter id':<15}"
264-
)
265-
print("-" * 180)
260+
def _display_awaited_by_tasks_table(table):
261+
"""Print the table in a simple tabular format."""
262+
print(_fmt_table_row(*_row_header))
263+
print('-' * 180)
266264
for row in table:
267-
print(f"{row[0]:<10} {row[1]:<20} {row[2]:<20} {row[3]:<50} {row[4]:<50} {row[5]:<15} {row[6]:<15}")
265+
print(_fmt_table_row(*row))
266+
267+
268+
def _fmt_table_row(tid, task_id, task_name, coro_stack,
269+
awaiter_chain, awaiter_name, awaiter_id):
270+
# Format a single row for the table format
271+
return (f'{tid:<10} {task_id:<20} {task_name:<20} {coro_stack:<50} '
272+
f'{awaiter_chain:<50} {awaiter_name:<15} {awaiter_id:<15}')
268273

269274

270-
def _display_awaited_by_tasks_csv(table, format: TaskTableOutputFormat) -> None:
271-
csv_header = ('tid', 'task id', 'task name', 'coroutine stack',
272-
'awaiter chain', 'awaiter name', 'awaiter id')
275+
def _display_awaited_by_tasks_csv(table, *, format):
276+
"""Print the table in CSV format"""
273277
if format == TaskTableOutputFormat.csv:
274278
delimiter = ','
275279
elif format == TaskTableOutputFormat.bsv:
276280
delimiter = '\N{BANANA}'
277281
else:
278282
raise ValueError(f"Unknown output format: {format}")
279283
csv_writer = csv.writer(sys.stdout, delimiter=delimiter)
280-
csv_writer.writerow(csv_header)
284+
csv_writer.writerow(_row_header)
281285
csv_writer.writerows(table)
282286

283287

0 commit comments

Comments
 (0)