Testing asyncio.tools.get_all_awaited_by()

As part of Export append_awaited_by API into public Python API. · Issue #134342 · python/cpython · GitHub, I’m just trying to test this new “feature”, though it is not a publicly stable API yet.

import asyncio
import asyncio.tools
import time
import threading

ev_terminated = threading.Event()

def debug_thread():
    while not ev_terminated.is_set():
        tasks = asyncio.tools.get_all_awaited_by(0)
        print(tasks)
        time.sleep(1)

async def main():
    while True:
        await asyncio.sleep(1)

if __name__ == "__main__":
    t = threading.Thread(target=debug_thread, daemon=True)
    t.start()
    try:
        asyncio.run(main())
    except BaseException:
        ev_terminated.set()
    finally:
        t.join()

results in:

(async-remote-debug) > python x-raw-server.py
Exception in thread Thread-1 (debug_thread):
FileNotFoundError: [Errno 2] No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/joongi/.pyenv/versions/3.14.0b1t/lib/python3.14t/threading.py", line 1079, in _bootstrap_inner
    self._context.run(self.run)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/home/joongi/.pyenv/versions/3.14.0b1t/lib/python3.14t/threading.py", line 1021, in run
    self._target(*self._args, **self._kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joongi/workspace/x-async-remote-debug/x-raw-server.py", line 12, in debug_thread
    tasks = asyncio.tools.get_all_awaited_by(0)
RuntimeError: Failed to find the PyRuntime section in the process.

I found that 3.14’s new remote debugging can attach to another process only when executed as root (sudo), and self-attaching seems not working as expected.

Though this is a beta release, I just want to check if self-attaching (pid 0) is an intended usecase or not.

Ah, maybe I need to do os.getpid() manually…
Since I’m moving to the airport, later I will check out..

and.. it works!

(async-remote-debug) > python x-raw-server.py
[(3521725, []), (3521672, [(2640889053712, 'Task-1', [])]), (0, [])]
[(3521725, []), (3521672, [(2640889053712, 'Task-1', [])]), (0, [])]
[(3521725, []), (3521672, [(2640889053712, 'Task-1', [])]), (0, [])]
[(3521725, []), (3521672, [(2640889053712, 'Task-1', [])]), (0, [])]
^C%