What does the following code output? import asyncio async def producer(queue): for i in range(3): await queue.put(i) await queue.put(None) async def consumer(queue): while True: item = await queue.get() if item is None: break print(item) async def main(): q = asyncio.Queue() await asyncio.gather(producer(q), consumer(q)) asyncio.run(main())

Python Professional Hard

Python Professional — Hard

What does the following code output? import asyncio async def producer(queue): for i in range(3): await queue.put(i) await queue.put(None) async def consumer(queue): while True: item = await queue.get() if item is None: break print(item) async def main(): q = asyncio.Queue() await asyncio.gather(producer(q), consumer(q)) asyncio.run(main())

Key points

  • The producer function adds values to the queue before adding None.
  • The consumer function prints the values in the order they were added.
  • The asyncio.gather() function runs both producer and consumer concurrently.

Ready to go further?

Related questions