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 8 · Async correctness patternsasyncio.Queue — bounded backpressure between producer and consumerpredict129 / 161
+150 XP
Task
📝 **Task:** Predict the exact 10-line output. The queue starts with maxsize=3, fills up, gets one item drained, accepts one more, then is fully drained. FIFO order is the central invariant — first in, first out. 📋 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


async def main() -> None:
    q: asyncio.Queue = asyncio.Queue(maxsize=3)

    await q.put("a")
    await q.put("b")
    await q.put("c")

    print(f"size after 3 puts: {q.qsize()}")
    print(f"full: {q.full()}")

    item = await q.get()
    q.task_done()
    print(f"got: {item}")
    print(f"size after 1 get: {q.qsize()}")

    await q.put("d")
    print(f"size after put d: {q.qsize()}")

    while not q.empty():
        item = await q.get()
        print(f"drained: {item}")
        q.task_done()

    print(f"final size: {q.qsize()}")
    print(f"empty: {q.empty()}")


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…