All quizzesMedium
Tasks & Concurrency — Series 2
Preview — 3 of 10 questions
What is the output of the following code?
javascript
import asyncio
async def main():
result = await asyncio.gather()
print(result)
asyncio.run(main())A[]
BTypeError: gather() missing 1 required positional argument
CThe program hangs forever waiting for at least one awaitable.
DNone
What is the output of the following code?
javascript
import asyncio
async def quick():
await asyncio.sleep(0.01)
return "quick done"
async def slow():
await asyncio.sleep(0.2)
return "slow done"
async def main():
t1 = asyncio.create_task(quick())
t2 = asyncio.create_task(slow())
done, pending = await asyncio.wait({t1, t2}, timeout=0.05)
print(len(done), len(pending))
print(list(done)[0].result())
asyncio.run(main())A2 0, quick done — asyncio.wait ignores the timeout argument unless every awaitable has already finished.
B1 1, quick done
C0 2, TypeError: 'set' object has no attribute 'result' — neither task has completed by the time the timeout elapses.
D1 1, slow done
What is the output of the following code?
javascript
import asyncio
async def compute():
await asyncio.sleep(0.05)
return "computed"
async def main():
task = asyncio.create_task(compute())
print(task.done())
await asyncio.sleep(0.1)
print(task.done())
print(task.result())
asyncio.run(main())ATrue, True, computed — a Task is immediately marked done the instant it's created.
BFalse, False, TypeError: result is not ready
CFalse, True, computed
DFalse, True, <coroutine object compute at 0x...> — .result() returns the underlying coroutine rather than its resolved value.
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.