GH-91048: Add utils for printing the call stack for asyncio tasks (#133284)

This commit is contained in:
Pablo Galindo Salgado 2025-05-04 02:51:57 +02:00 committed by GitHub
parent 7363e8d24d
commit 2bc8365231
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 1309 additions and 90 deletions

View file

@ -1,5 +1,7 @@
import argparse
import ast
import asyncio
import asyncio.tools
import concurrent.futures
import contextvars
import inspect
@ -140,6 +142,36 @@ class REPLThread(threading.Thread):
if __name__ == '__main__':
parser = argparse.ArgumentParser(
prog="python3 -m asyncio",
description="Interactive asyncio shell and CLI tools",
)
subparsers = parser.add_subparsers(help="sub-commands", dest="command")
ps = subparsers.add_parser(
"ps", help="Display a table of all pending tasks in a process"
)
ps.add_argument("pid", type=int, help="Process ID to inspect")
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)
sys.exit(0)
case "pstree":
asyncio.tools.display_awaited_by_tasks_tree(args.pid)
sys.exit(0)
case None:
pass # continue to the interactive shell
case _:
# shouldn't happen as an invalid command-line wouldn't parse
# but let's keep it for the next person adding a command
print(f"error: unhandled command {args.command}", file=sys.stderr)
parser.print_usage(file=sys.stderr)
sys.exit(1)
sys.audit("cpython.run_stdin")
if os.getenv('PYTHON_BASIC_REPL'):