Asynchronous Programming

Asynchronous programming is a concurrency model that allows a single thread to handle thousands of concurrent tasks by "awaiting" I/O operations instead of blocking. In Python, this is achieved via the asyncio library and the async / await syntax.


1. The Event Loop Mechanism

Unlike multi-threading, which relies on the operating system to switch between threads, asyncio uses a single-threaded Event Loop.


2. Basic Syntax: async and await

To make a function asynchronous, you define it with async def. To wait for its result, you use the await keyword.

import asyncio

async def fetch_api():
    print("Start fetching...")
    await asyncio.sleep(1) # Simulated network delay
    print("Done!")
    return {"data": 123}

# Entry point
async def main():
    result = await fetch_api()

asyncio.run(main())

3. Running Tasks in Parallel

The real power of asyncio comes from running multiple coroutines simultaneously using asyncio.gather.

async def main():
    # Runs all three tasks concurrently
    results = await asyncio.gather(
        fetch_api(),
        fetch_api(),
        fetch_api()
    )

4. Asyncio vs. Multithreading


Interview Pro-Tips: The await keyword

If an interviewer asks what await actually does:


Technical Summary

  1. Coroutine: A function that can be paused and resumed (async def).
  2. Event Loop: The central manager of all asynchronous tasks.
  3. Best Use Case: High-concurrency web servers (e.g., FastAPI), scrapers, and chat applications.