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 divescontextvars in async — Task context isolationpredict108 / 161
+150 XP
Task
📝 **Task:** Predict the exact output. \`req_id\` is set to \`"req-A"\` in \`main\`. Then: (1) \`await child()\` — does the child see "req-A" or the default? (2) \`asyncio.create_task(isolated())\` where isolated does \`set("req-B")\` — does its change leak back to main? (3) After both, what does main see? 📋 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 contextvars

req_id = contextvars.ContextVar("req_id", default="none")


async def child():
    return f"child:{req_id.get()}"


async def isolated():
    req_id.set("req-B")
    return f"isolated:{req_id.get()}"


async def main():
    req_id.set("req-A")
    a = await child()
    t = asyncio.create_task(isolated())
    b = await t
    return [a, b, f"main:{req_id.get()}"]


print(asyncio.run(main()))

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…