title: How to tell where an asyncio task is waiting?
body: <p>I have rather complex system running an asynchronous task called &quot;automation&quot;. Meanwhile I would like to inspect where the task is currently waiting. Something like a callstack for async-await.</p>
  <p>The following example creates such an <code>automation</code> task, stepping into <code>do_something</code> which in turn calls <code>sleep</code>. While this task is running, its stack is printed. I'd wish to see something like &quot;automation → do_something → sleep&quot;. But <code>print_stack</code> only points to the line <code>await do_something()</code> in the top-level coroutine <code>automation</code>, but nothing more.</p>
  <pre class="lang-py prettyprint-override"><code>#!/usr/bin/env python3
  import asyncio
  
  async def sleep():
      await asyncio.sleep(0.1)
  
  async def do_something():
      print('...')
      await sleep()
  
  async def automation():
      for _ in range(10):
          await do_something()
  
  async def main():
      task = asyncio.create_task(automation(), name='automation')
      while not task.done():
          task.print_stack()
          await asyncio.sleep(0.1)
  
  asyncio.run(main())
  </code></pre>
  <p>I thought about using <code>_scheduled</code> from <code>asyncio.BaseEventLoop</code>, but this seems to be always <code>[]</code> in my example. And since my production code runs uvloop I looked into <a href="https://github.com/MagicStack/uvloop/issues/135" rel="nofollow noreferrer">https://github.com/MagicStack/uvloop/issues/135</a>,
  <a href="https://github.com/MagicStack/uvloop/issues/163" rel="nofollow noreferrer">https://github.com/MagicStack/uvloop/issues/163</a> and
  <a href="https://github.com/MagicStack/uvloop/pull/171" rel="nofollow noreferrer">https://github.com/MagicStack/uvloop/pull/171</a>, all of which are stale for about 4 years.</p>
  <p>Is there something else I could try?</p>