Skip to content

Commit cf70073

Browse files
committed
Add CSV and 🍌SV output formats to asyncio ps
1 parent 393773a commit cf70073

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

Lib/asyncio/__main__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,16 @@ def interrupt(self) -> None:
153153
"ps", help="Display a table of all pending tasks in a process"
154154
)
155155
ps.add_argument("pid", type=int, help="Process ID to inspect")
156+
formats = [fmt.value for fmt in asyncio.tools.TaskTableOutputFormat]
157+
ps.add_argument("--format", choices=formats, default="table")
156158
pstree = subparsers.add_parser(
157159
"pstree", help="Display a tree of all pending tasks in a process"
158160
)
159161
pstree.add_argument("pid", type=int, help="Process ID to inspect")
160162
args = parser.parse_args()
161163
match args.command:
162164
case "ps":
163-
asyncio.tools.display_awaited_by_tasks_table(args.pid)
165+
asyncio.tools.display_awaited_by_tasks_table(args.pid, args.format)
164166
sys.exit(0)
165167
case "pstree":
166168
asyncio.tools.display_awaited_by_tasks_tree(args.pid)

Lib/asyncio/tools.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from enum import Enum
77
import sys
88
from _remote_debugging import get_all_awaited_by
9+
import csv
910

1011

1112
class NodeType(Enum):
@@ -200,20 +201,45 @@ def _get_awaited_by_tasks(pid: int) -> list:
200201
sys.exit(1)
201202

202203

203-
def display_awaited_by_tasks_table(pid: int) -> None:
204+
class TaskTableOutputFormat(Enum):
205+
table = "table"
206+
csv = "csv"
207+
bsv = "bsv"
208+
209+
210+
_header = ('tid', 'task id', 'task name', 'coroutine chain', 'awaiter name', 'awaiter id')
211+
212+
213+
def display_awaited_by_tasks_table(pid: int, format_: TaskTableOutputFormat = TaskTableOutputFormat.table) -> None:
204214
"""Build and print a table of all pending tasks under `pid`."""
205215

206216
tasks = _get_awaited_by_tasks(pid)
207217
table = build_task_table(tasks)
218+
if format_ != TaskTableOutputFormat.table:
219+
_display_awaited_by_tasks_csv(table, format_)
220+
return
208221
# Print the table in a simple tabular format
209222
print(
210-
f"{'tid':<10} {'task id':<20} {'task name':<20} {'coroutine chain':<50} {'awaiter name':<20} {'awaiter id':<15}"
223+
f"{_header[0]:<10} {_header[1]:<20} {_header[2]:<20} {_header[3]:<50} {_header[4]:<20} {_header[5]:<15}"
211224
)
212225
print("-" * 135)
213226
for row in table:
214227
print(f"{row[0]:<10} {row[1]:<20} {row[2]:<20} {row[3]:<50} {row[4]:<20} {row[5]:<15}")
215228

216229

230+
def _display_awaited_by_tasks_csv(table, format_: TaskTableOutputFormat) -> None:
231+
match format_:
232+
case TaskTableOutputFormat.csv:
233+
delimiter = ','
234+
case TaskTableOutputFormat.bsv:
235+
delimiter = '🍌'
236+
case _:
237+
raise ValueError(f"Unknown output format: {format_}")
238+
csv_writer = csv.writer(sys.stdout, delimiter=delimiter)
239+
csv_writer.writerow(_header)
240+
csv_writer.writerows(table)
241+
242+
217243
def display_awaited_by_tasks_tree(pid: int) -> None:
218244
"""Build and print a tree of all pending tasks under `pid`."""
219245

0 commit comments

Comments
 (0)