1
1
"""Tools to analyze tasks running in asyncio programs."""
2
2
3
- from collections import defaultdict , namedtuple
3
+ from collections import defaultdict
4
4
import csv
5
5
from itertools import count
6
6
from enum import Enum , StrEnum , auto
7
7
import sys
8
8
from _remote_debugging import RemoteUnwinder , FrameInfo
9
9
10
-
11
10
class NodeType (Enum ):
12
11
COROUTINE = 1
13
12
TASK = 2
@@ -242,10 +241,7 @@ class TaskTableOutputFormat(StrEnum):
242
241
# https://www.youtube.com/watch?v=RrsVi1P6n0w
243
242
244
243
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 ):
249
245
"""Build and print a table of all pending tasks under `pid`."""
250
246
251
247
tasks = _get_awaited_by_tasks (pid )
@@ -254,30 +250,38 @@ def display_awaited_by_tasks_table(
254
250
if format == TaskTableOutputFormat .table :
255
251
_display_awaited_by_tasks_table (table )
256
252
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' )
258
258
259
259
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 )
266
264
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} ' )
268
273
269
274
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"""
273
277
if format == TaskTableOutputFormat .csv :
274
278
delimiter = ','
275
279
elif format == TaskTableOutputFormat .bsv :
276
280
delimiter = '\N{BANANA} '
277
281
else :
278
282
raise ValueError (f"Unknown output format: { format } " )
279
283
csv_writer = csv .writer (sys .stdout , delimiter = delimiter )
280
- csv_writer .writerow (csv_header )
284
+ csv_writer .writerow (_row_header )
281
285
csv_writer .writerows (table )
282
286
283
287
0 commit comments