InterviewHubPRO
Section Overview
Q39

Question 39 of 65

Part 3: Asynchronous Programming & Concurrency (26 - 40)

60%
Python & FastAPI•Part 3: Asynchronous Programming & Concurrency (26 - 40)

Producer-Consumer with `asyncio.Queue`:

In Simple Words: Decouples producers generating work from worker consumers processing work concurrently with flow control.

python
import asyncio

async def producer(queue: asyncio.Queue):
    for i in range(5):
        await queue.put(i)
        print(f"Produced: {i}")

async def consumer(queue: asyncio.Queue):
    while True:
        item = await queue.get()
        print(f"Consumed: {item}")
        queue.task_done()