What does the following async code output? import asyncio async def task(n): await asyncio.sleep(0) return n * 2 async def main(): results = await asyncio.gather(task(1), task(2), task(3)) print(results) asyncio.run(main())

Python Professional Hard

Python Professional — Hard

What does the following async code output? import asyncio async def task(n): await asyncio.sleep(0) return n * 2 async def main(): results = await asyncio.gather(task(1), task(2), task(3)) print(results) asyncio.run(main())

Key points

  • asyncio.gather() collects results in the order of tasks passed
  • Each task returns n * 2
  • The results list will contain the doubled values of 1, 2, and 3
  • The output will be [2, 4, 6]

Ready to go further?

Related questions