Bird
0
0

Given the code below, what will be the output?

medium📝 Predict Output Q13 of 15
Agentic AI - Production Agent Architecture
Given the code below, what will be the output?
import asyncio

async def agent1():
    await asyncio.sleep(1)
    return 'Agent1 done'

async def agent2():
    await asyncio.sleep(2)
    return 'Agent2 done'

async def main():
    results = await asyncio.gather(agent1(), agent2())
    print(results)

asyncio.run(main())
A['Agent1 done', 'Agent2 done'] after about 2 seconds
B['Agent2 done', 'Agent1 done'] after about 2 seconds
C['Agent1 done', 'Agent2 done'] after about 3 seconds
DError because agent2 takes longer
Step-by-Step Solution
Solution:
  1. Step 1: Understand asyncio.gather timing

    asyncio.gather runs tasks concurrently, so total time is max of individual times.
  2. Step 2: Analyze sleep durations

    agent1 sleeps 1s, agent2 sleeps 2s, so total time ~2 seconds, results in order of calls.
  3. Final Answer:

    ['Agent1 done', 'Agent2 done'] after about 2 seconds -> Option A
  4. Quick Check:

    Concurrent run time = max sleep = 2s [OK]
Quick Trick: Total time = longest agent sleep with asyncio.gather [OK]
Common Mistakes:
  • Adding sleep times instead of taking max
  • Assuming output order changes by sleep time
  • Expecting error due to different sleep durations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes