Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Lib/asyncio/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,22 @@ def interrupt(self) -> None:
"ps", help="Display a table of all pending tasks in a process"
)
ps.add_argument("pid", type=int, help="Process ID to inspect")
formats = [fmt.value for fmt in asyncio.tools.TaskTableOutputFormat]
big_secret = asyncio.tools.TaskTableOutputFormat.bsv.value
formats_to_show = [
fmt for fmt in formats if fmt != big_secret
]
formats_to_show_str = f"{{{','.join(formats_to_show)}}}"
ps.add_argument("--format", choices=formats, default="table",
metavar=formats_to_show_str)
pstree = subparsers.add_parser(
"pstree", help="Display a tree of all pending tasks in a process"
)
pstree.add_argument("pid", type=int, help="Process ID to inspect")
args = parser.parse_args()
match args.command:
case "ps":
asyncio.tools.display_awaited_by_tasks_table(args.pid)
asyncio.tools.display_awaited_by_tasks_table(args.pid, args.format)
sys.exit(0)
case "pstree":
asyncio.tools.display_awaited_by_tasks_tree(args.pid)
Expand Down
39 changes: 37 additions & 2 deletions Lib/asyncio/tools.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Tools to analyze tasks running in asyncio programs."""

from collections import defaultdict, namedtuple
import csv
from itertools import count
from enum import Enum
from enum import Enum, StrEnum, auto
import sys
from _remote_debugging import RemoteUnwinder, FrameInfo


class NodeType(Enum):
COROUTINE = 1
TASK = 2
Expand Down Expand Up @@ -232,11 +234,30 @@ def _get_awaited_by_tasks(pid: int) -> list:
sys.exit(1)


def display_awaited_by_tasks_table(pid: int) -> None:
class TaskTableOutputFormat(StrEnum):
table = auto()
csv = auto()
bsv = auto()
# 🍌SV is not just a format. It's a lifestyle. A philosophy.
# https://www.youtube.com/watch?v=RrsVi1P6n0w


def display_awaited_by_tasks_table(
pid: int,
format: TaskTableOutputFormat | str = TaskTableOutputFormat.table
) -> None:
"""Build and print a table of all pending tasks under `pid`."""

tasks = _get_awaited_by_tasks(pid)
table = build_task_table(tasks)
format = TaskTableOutputFormat(format)
if format == TaskTableOutputFormat.table:
_display_awaited_by_tasks_table(table)
else:
_display_awaited_by_tasks_csv(table, format)


def _display_awaited_by_tasks_table(table) -> None:
# Print the table in a simple tabular format
print(
f"{'tid':<10} {'task id':<20} {'task name':<20} {'coroutine stack':<50} {'awaiter chain':<50} {'awaiter name':<15} {'awaiter id':<15}"
Expand All @@ -246,6 +267,20 @@ def display_awaited_by_tasks_table(pid: int) -> None:
print(f"{row[0]:<10} {row[1]:<20} {row[2]:<20} {row[3]:<50} {row[4]:<50} {row[5]:<15} {row[6]:<15}")


def _display_awaited_by_tasks_csv(table, format: TaskTableOutputFormat) -> None:
csv_header = ('tid', 'task id', 'task name', 'coroutine stack',
'awaiter chain', 'awaiter name', 'awaiter id')
if format == TaskTableOutputFormat.csv:
delimiter = ','
elif format == TaskTableOutputFormat.bsv:
delimiter = '\N{BANANA}'
else:
raise ValueError(f"Unknown output format: {format}")
csv_writer = csv.writer(sys.stdout, delimiter=delimiter)
csv_writer.writerow(csv_header)
csv_writer.writerows(table)


def display_awaited_by_tasks_tree(pid: int) -> None:
"""Build and print a tree of all pending tasks under `pid`."""

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add CSV as an output format for :program:`python -m asyncio ps`.
Loading