Overview:
-
The get_stack() method returns the stack frame for the wrapped coroutine of the asyncio.Task, at the place of its suspension.
-
By the time get_stack() is executed, if the Task is already complete or cancelled no stack frame is returned.
-
When the wrapped coroutine of the Task is exited due to an exception, the get_stack() method returns a list of traceback frames.
Example - get_stack() returning a list of traceback frames:
# Example Python program that prints the stack frames # A coroutine defintion # Yet another coroutine defintion # A coroutine that creates an asyncio.Task # Get the stack of the task and print it # Create and event loop and run the task |
Output:
Inside samplecoro: 1 Inside simplecoro: 1 [<frame at 0x1044718c0, file '/Users/vinodh/pprogs/GetStockEx1.py', line 23, code samplecoro>, <frame at 0x104471540, file '/Users/vinodh/pprogs/GetStockEx1.py', line 13, code simplecoro>] Handled exception:Thrown deliberately |
Example - get_stack() returning the stack frame of a suspended coroutine:
# Example Python program that prints the # Define a coroutine # Coroutine that creates and executes Task # Get stack of the wrapped coroutine # Print the task completion status # Run the coroutine |
Output:
[<frame at 0x10fdfedc0, file '/Users/kpji/getstack_suspend.py', line 7, code coro>] 1 3 5 7 9 Task completed:True |