MCS-201 · Unit 15

Python Advanced Concepts

Section 5 of 5

Block 4 · Unit 15Python Advanced Concepts9 min · Section 5 of 5

15.5Asynchronous Coroutines with asyncio

Covers IGNOU MCS-201 §15.5

Modern Python writes coroutines with the keywords async and await, and runs them with the standard asyncio library. This is how high-performance web servers, chat servers and database drivers handle thousands of connections in one process.

async and await

ElementMeaning
async def f():Defines a coroutine function; calling f() returns a coroutine object without running it
await xInside an async def: pause this coroutine until x (a coroutine, task or other awaitable) finishes, letting other tasks run meanwhile
asyncio.run(main())Starts the event loop, runs the top-level coroutine main() to completion, then closes the loop
asyncio.create_task(coro)Schedules a coroutine to run concurrently as a task; returns a Task that can be awaited later
asyncio.gather(c1, c2, ...)Runs several awaitables concurrently and returns their results as a list
asyncio.sleep(s)A non-blocking pause: other tasks run while this one waits (unlike time.sleep, which blocks everything)

The event loop is the scheduler: it keeps a list of tasks, runs whichever is ready, and switches when the running one reaches an await.

Key takeaways

  • async def creates coroutine functions; await suspends until an awaitable completes.
  • asyncio.run() runs the top-level coroutine on an event loop.
  • create_task() and gather() run coroutines concurrently.
  • Concurrency helps when tasks wait (I/O); use asyncio.sleep, not time.sleep.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What does calling an async def function return?

Show answer

Answer: B. A coroutine object, which runs only when awaited or scheduled

The body does not run until the coroutine is awaited, passed to asyncio.run, or wrapped in a task.

Question 2 · multiple choice

Which function starts the event loop for a top-level coroutine main()?

Show answer

Answer: C. asyncio.run(main())

asyncio.run creates the loop, runs main() to completion and closes the loop.

Question 3 · multiple choice

Two coroutines each wait 1 second with asyncio.sleep(1). About how long does await asyncio.gather(a(), b()) take?

Show answer

Answer: A. About 1 second

Both waits overlap in the event loop; the total is the longest single wait.

Question 4 · multiple choice

What does this program print?

Python · quiz_async.py
import asyncio

async def square(n):
    await asyncio.sleep(0)
    return n * n

async def main():
    results = await asyncio.gather(square(2), square(3), square(4))
    print(results)

asyncio.run(main())
Show answer

Answer: D. [4, 9, 16]

gather runs the three coroutines and returns their results in the order given.

Question 5 · multiple choice

Why should time.sleep() not be used inside a coroutine?

Show answer

Answer: C. It blocks the whole event loop, so no other task can run meanwhile

asyncio.sleep() pauses only the current coroutine and lets the loop run others.

Question 6 · short answer

Write a program with a coroutine fetch(name, seconds) that waits for the given time and returns a message, and a main() that runs three fetches concurrently and prints their results.

Show worked answer

import asyncio, then async def fetch(name, seconds): await asyncio.sleep(seconds); return f"{name} done", then async def main(): results = await asyncio.gather(fetch("A", 1), fetch("B", 2), fetch("C", 1)); print(results), and finally asyncio.run(main()). It prints ['A done', 'B done', 'C done'] after about 2 seconds (the longest wait), instead of 4 seconds if the fetches were awaited one after another.

↑ ↓ move↵ openTab switch type