Skip to main content
🔒 Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial →
← CoursesSenior Deep-DivesModule 6 · FAANG-2026 deep divesasyncio reentrance — you can't nest `asyncio.run()`predict110 / 161
+150 XP
Task
📝 **Task:** Predict the exact output. Inside \`outer\` (running in an event loop), three things happen: (1) we check whether a loop is running, (2) we try to nest \`asyncio.run(inner())\` and catch the RuntimeError, (3) we call the same \`inner()\` correctly via \`await\`. Trace each print. 📋 Implement the function above. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Predict the output

Read the code carefully

import asyncio
import warnings

# Silence the "coroutine never awaited" warning that fires when
# the inner asyncio.run() raises before scheduling its coroutine.
warnings.simplefilter("ignore", RuntimeWarning)


async def inner():
    return "inner-result"


async def outer():
    loop = asyncio.get_running_loop()
    print(f"loop_running:{loop.is_running()}")
    try:
        asyncio.run(inner())
        print("nested:ok")
    except RuntimeError as e:
        print(f"nested:{e}")
    result = await inner()
    print(f"awaited:{result}")


asyncio.run(outer())

What will the program print? Write here:

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…