Skip to content

Instantly share code, notes, and snippets.

@mpage
Created September 22, 2022 18:52
Show Gist options
  • Save mpage/584a02fc986d32b11b290c7032700369 to your computer and use it in GitHub Desktop.
Save mpage/584a02fc986d32b11b290c7032700369 to your computer and use it in GitHub Desktop.
import asyncio
from asyncio.tasks import Task
from test.support.cinder import get_await_stack
async def f1():
return await f2()
async def f2():
# Create a new task for f3()
return await asyncio.ensure_future(f3())
async def f3():
return await f4()
def dump_task_stack():
print("Stack retrieved from Task.get_stack():")
stack = asyncio.current_task().get_stack()
for frame in stack:
print(f" {frame.f_code.co_name}")
def dump_await_stack():
print("Stack retrieved using awaiters:")
coro = asyncio.current_task()._coro
stack = [coro] + get_await_stack(coro)
for coro in reversed(stack):
print(f" {coro.cr_code.co_name}")
async def f4():
await asyncio.sleep(1)
dump_task_stack()
dump_await_stack()
return 42
if __name__ == "__main__":
asyncio.run(f1())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment