Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-134861: Add CSV and 🍌SV output formats to asyncio ps #134862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
42 changes: 40 additions & 2 deletions Lib/asyncio/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from enum import Enum
import sys
from _remote_debugging import RemoteUnwinder, FrameInfo
import csv


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


def display_awaited_by_tasks_table(pid: int) -> None:
class TaskTableOutputFormat(Enum):
table = "table"
csv = "csv"
bsv = "bsv"
# As per the words of the asyncio 🍌SV spec lead:
# > 🍌SV is not just a format. It’s a lifestyle. A philosophy.
# https://www.youtube.com/watch?v=RrsVi1P6n0w


_header = ('tid', 'task id', 'task name', 'coroutine stack', 'awaiter chain', 'awaiter name', 'awaiter id')


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}"
f"{_header[0]:<10} {_header[1]:<20} {_header[2]:<20} {_header[3]:<50} {_header[4]:<50} {_header[5]:<15} {_header[6]:<15}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try storing the numbers in either another array or a tulle with headers and for loop this print.

)
print("-" * 180)
for row in table:
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:
match format_:
case TaskTableOutputFormat.csv:
delimiter = ','
case TaskTableOutputFormat.bsv:
delimiter = '\N{BANANA}'
case _:
raise ValueError(f"Unknown output format: {format_}")
csv_writer = csv.writer(sys.stdout, delimiter=delimiter)
csv_writer.writerow(_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,3 @@
Add CSV output format to asyncio ps

Absolutely no other output format was added in this PR 🍌
Loading