@@ -18,6 +18,64 @@ a suspended *future*.
18
18
.. versionadded :: 3.14
19
19
20
20
21
+ .. function :: print_call_stack(*, future=None, file=None)
22
+
23
+ Print the async call stack for the current task or the provided
24
+ :class: `Task ` or :class: `Future `.
25
+
26
+ The function recieves an optional keyword-only *future * argument.
27
+ If not passed, the current running task will be used. If there's no
28
+ current task, the function returns ``None ``.
29
+
30
+ If *file * is not specified the function will print to :data: `sys.stdout `.
31
+
32
+ **Example: **
33
+
34
+ The following Python code:
35
+
36
+ .. code-block :: python
37
+
38
+ import asyncio
39
+
40
+ async def test ():
41
+ asyncio.print_call_stack()
42
+
43
+ async def main ():
44
+ async with asyncio.TaskGroup() as g:
45
+ g.create_task(test())
46
+
47
+ asyncio.run(main())
48
+
49
+ will print::
50
+
51
+ * Task(name='Task-2', id=0x105038fe0)
52
+ + Call stack:
53
+ | * print_call_stack()
54
+ | asyncio/stack.py:231
55
+ | * async test()
56
+ | test.py:4
57
+ + Awaited by:
58
+ * Task(name='Task-1', id=0x1050a6060)
59
+ + Call stack:
60
+ | * async TaskGroup.__aexit__()
61
+ | asyncio/taskgroups.py:107
62
+ | * async main()
63
+ | test.py:7
64
+
65
+ For rendering the call stack to a string the following pattern
66
+ should be used:
67
+
68
+ .. code-block :: python
69
+
70
+ import io
71
+
72
+ ...
73
+
74
+ buf = io.StringIO()
75
+ asyncio.print_call_stack(file = buf)
76
+ output = buf.getvalue()
77
+
78
+
21
79
.. function :: capture_call_stack(*, future=None)
22
80
23
81
Capture the async call stack for the current task or the provided
@@ -49,17 +107,6 @@ a suspended *future*.
49
107
Where ``coroutine `` is a coroutine object of an awaiting coroutine
50
108
or asyncronous generator.
51
109
52
- .. function :: print_call_stack(*, future=None, file=None)
53
-
54
- Print the async call stack for the current task or the provided
55
- :class: `Task ` or :class: `Future `.
56
-
57
- The function recieves an optional keyword-only *future * argument.
58
- If not passed, the current running task will be used. If there's no
59
- current task, the function returns ``None ``.
60
-
61
- If *file * is not specified the function will print to :data: `sys.stdout `.
62
-
63
110
64
111
Low level utility functions
65
112
===========================
0 commit comments