@@ -541,26 +541,21 @@ For example given this code:
541541
542542 import asyncio
543543
544- async def sleeper ( name , delay ):
545- await asyncio.sleep(delay )
546- print (f " { name } is done sleeping. " )
544+ async def play ( track ):
545+ await asyncio.sleep(5 )
546+ print (f " 🎵 Finished: { track } " )
547547
548- async def inner (name ):
549- await asyncio.sleep(0.1 )
550- await sleeper(name, 1 )
551-
552- async def task_group (name ):
553- await asyncio.gather(
554- inner(f " { name} -1 " ),
555- inner(f " { name} -2 " ),
556- )
548+ async def album (name , tracks ):
549+ async with asyncio.TaskGroup() as tg:
550+ for track in tracks:
551+ tg.create_task(play(track), name = track)
557552
558553 async def main ():
559- # Start two separate task groups
560- t1 = asyncio .create_task(task_group( " groupA " ))
561- t2 = asyncio.create_task(task_group( " groupB " ) )
562- await t1
563- await t2
554+ async with asyncio.TaskGroup() as tg:
555+ tg .create_task(
556+ album( " Sundowning " , [ " TNDNBTG " , " Levitate " ]), name = " Sundowning " )
557+ tg.create_task(
558+ album( " TMBTE " , [ " DYWTYLM " , " Aqua Regia " ]), name = " TMBTE " )
564559
565560 if __name__ == " __main__" :
566561 asyncio.run(main())
@@ -571,13 +566,15 @@ Executing the new tool on the running process will yield a table like this:
571566
572567 python -m asyncio ps 12345
573568
574- tid task id task name coroutine chain awaiter name awaiter id
569+ tid task id task name coroutine chain awaiter name awaiter id
575570 ---------------------------------------------------------------------------------------------------------------------------------------
576- 6826911 0x200013c0220 Task-2 main Task-1 0x200013b0020
577- 6826911 0x200013c0620 Task-4 task_group Task-2 0x200013c0220
578- 6826911 0x200013c0820 Task-5 task_group Task-2 0x200013c0220
579- 6826911 0x200013c0c20 Task-6 task_group Task-3 0x200013c0420
580- 6826911 0x200013c0e20 Task-7 task_group Task-3 0x200013c0420
571+ 8138752 0x564bd3d0210 Task-1 0x0
572+ 8138752 0x564bd3d0410 Sundowning _aexit -> __aexit__ -> main Task-1 0x564bd3d0210
573+ 8138752 0x564bd3d0610 TMBTE _aexit -> __aexit__ -> main Task-1 0x564bd3d0210
574+ 8138752 0x564bd3d0810 TNDNBTG _aexit -> __aexit__ -> album Sundowning 0x564bd3d0410
575+ 8138752 0x564bd3d0a10 Levitate _aexit -> __aexit__ -> album Sundowning 0x564bd3d0410
576+ 8138752 0x564bd3e0550 DYWTYLM _aexit -> __aexit__ -> album TMBTE 0x564bd3d0610
577+ 8138752 0x564bd3e0710 Aqua Regia _aexit -> __aexit__ -> album TMBTE 0x564bd3d0610
581578
582579
583580 or:
@@ -587,15 +584,21 @@ or:
587584 python -m asyncio pstree 12345
588585
589586 └── (T) Task-1
590- └── main
591- └── (T) Task-2
592- └── task_group
593- ├── (T) Task-4
594- └── (T) Task-5
595- └── (T) Task-3
596- └── task_group
597- ├── (T) Task-6
598- └── (T) Task-7
587+ └── main
588+ └── __aexit__
589+ └── _aexit
590+ ├── (T) Sundowning
591+ │ └── album
592+ │ └── __aexit__
593+ │ └── _aexit
594+ │ ├── (T) TNDNBTG
595+ │ └── (T) Levitate
596+ └── (T) TMBTE
597+ └── album
598+ └── __aexit__
599+ └── _aexit
600+ ├── (T) DYWTYLM
601+ └── (T) Aqua Regia
599602
600603 If a cycle is detected in the async await graph (which could indicate a
601604programming issue), the tool raises an error and lists the cycle paths that
0 commit comments