Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
GH-91048: Add utils for capturing async call stack for asyncio programs and enable profiling#124640
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
Uh oh!
There was an error while loading. Please reload this page.
GH-91048: Add utils for capturing async call stack for asyncio programs and enable profiling #124640
Changes from all commits
1b01a910fc55111d20a51c8be18eabf2cb920ceab772d9321c9475f6e1099e9817f88b54386ac98434f0485c1668802be71ddc9cfbc9beb8fd141d42d72f24391defad6357fdbb3b6df54c99ecc1a4f09027d522c2d5ec608d09ebfe3113b18ec26de4cc462d5cdc3683606f25edac418dc6d3430884ea131765881b0a31258ce3db9ecefbb77dcb0886794687d2524b47bef1230b7ecb1d6158ac51364c7e59eb9eba5e159121f6f8f48f074c5ad1067c0439f04911077480530484931f42873779939103ed5c121f9ea98a43dfab3fae68d0aedf0df0032a0ce241b8f126f6966d84ef56468a404b88a911fed8ab511a4c3c685a785adeba577328064129ace332d9d6d943f703ff46e8678639cb5b2961b2b7b9533ab9596191dad9152e066bf214caeec438f061da8dd667cf8f5e5eda9c7cFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| .. currentmodule:: asyncio | ||
| .. _asyncio-graph: | ||
| ======================== | ||
| Call Graph Introspection | ||
| ======================== | ||
| **Source code:** :source:`Lib/asyncio/graph.py` | ||
| ------------------------------------- | ||
| asyncio has powerful runtime call graph introspection utilities | ||
| to trace the entire call graph of a running *coroutine* or *task*, or | ||
| a suspended *future*. These utilities and the underlying machinery | ||
| can be used from within a Python program or by external profilers | ||
| and debuggers. | ||
| .. versionadded:: next | ||
| .. function:: print_call_graph(future=None, /, *, file=None, depth=1, limit=None) | ||
| Print the async call graph for the current task or the provided | ||
| :class:`Task` or :class:`Future`. | ||
| This function prints entries starting from the top frame and going | ||
ambv marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| down towards the invocation point. | ||
| The function receives an optional *future* argument. | ||
| If not passed, the current running task will be used. | ||
ambv marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| If the function is called on *the current task*, the optional | ||
| keyword-only *depth* argument can be used to skip the specified | ||
| number of frames from top of the stack. | ||
| If the optional keyword-only *limit* argument is provided, each call stack | ||
| in the resulting graph is truncated to include at most ``abs(limit)`` | ||
| entries. If *limit* is positive, the entries left are the closest to | ||
| the invocation point. If *limit* is negative, the topmost entries are | ||
| left. If *limit* is omitted or ``None``, all entries are present. | ||
| If *limit* is ``0``, the call stack is not printed at all, only | ||
| "awaited by" information is printed. | ||
| If *file* is omitted or ``None``, the function will print | ||
| to :data:`sys.stdout`. | ||
| **Example:** | ||
| The following Python code: | ||
| .. code-block:: python | ||
| import asyncio | ||
| async def test(): | ||
| asyncio.print_call_graph() | ||
| async def main(): | ||
| async with asyncio.TaskGroup() as g: | ||
| g.create_task(test()) | ||
| asyncio.run(main()) | ||
| will print:: | ||
| * Task(name='Task-2', id=0x1039f0fe0) | ||
| + Call stack: | ||
| | File 't2.py', line 4, in async test() | ||
| + Awaited by: | ||
| * Task(name='Task-1', id=0x103a5e060) | ||
| + Call stack: | ||
| | File 'taskgroups.py', line 107, in async TaskGroup.__aexit__() | ||
| | File 't2.py', line 7, in async main() | ||
| .. function:: format_call_graph(future=None, /, *, depth=1, limit=None) | ||
| Like :func:`print_call_graph`, but returns a string. | ||
| If *future* is ``None`` and there's no current task, | ||
| the function returns an empty string. | ||
| .. function:: capture_call_graph(future=None, /, *, depth=1, limit=None) | ||
| Capture the async call graph for the current task or the provided | ||
| :class:`Task` or :class:`Future`. | ||
| The function receives an optional *future* argument. | ||
| If not passed, the current running task will be used. If there's no | ||
| current task, the function returns ``None``. | ||
| If the function is called on *the current task*, the optional | ||
| keyword-only *depth* argument can be used to skip the specified | ||
| number of frames from top of the stack. | ||
| Returns a ``FutureCallGraph`` data class object: | ||
| * ``FutureCallGraph(future, call_stack, awaited_by)`` | ||
| Where *future* is a reference to a :class:`Future` or | ||
| a :class:`Task` (or their subclasses.) | ||
| ``call_stack`` is a tuple of ``FrameCallGraphEntry`` objects. | ||
| ``awaited_by`` is a tuple of ``FutureCallGraph`` objects. | ||
| * ``FrameCallGraphEntry(frame)`` | ||
| Where *frame* is a frame object of a regular Python function | ||
| in the call stack. | ||
| Low level utility functions | ||
| =========================== | ||
| To introspect an async call graph asyncio requires cooperation from | ||
| control flow structures, such as :func:`shield` or :class:`TaskGroup`. | ||
| Any time an intermediate :class:`Future` object with low-level APIs like | ||
| :meth:`Future.add_done_callback() <asyncio.Future.add_done_callback>` is | ||
| involved, the following two functions should be used to inform asyncio | ||
| about how exactly such intermediate future objects are connected with | ||
| the tasks they wrap or control. | ||
| .. function:: future_add_to_awaited_by(future, waiter, /) | ||
| Record that *future* is awaited on by *waiter*. | ||
| Both *future* and *waiter* must be instances of | ||
| :class:`Future` or :class:`Task` or their subclasses, | ||
| otherwise the call would have no effect. | ||
| A call to ``future_add_to_awaited_by()`` must be followed by an | ||
| eventual call to the :func:`future_discard_from_awaited_by` function | ||
| with the same arguments. | ||
| .. function:: future_discard_from_awaited_by(future, waiter, /) | ||
| Record that *future* is no longer awaited on by *waiter*. | ||
| Both *future* and *waiter* must be instances of | ||
| :class:`Future` or :class:`Task` or their subclasses, otherwise | ||
| the call would have no effect. | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.