Glossary · Python

asyncio

Also known as: async await python, event loop, coroutine

Python's standard library for writing concurrent I/O code with async/await, running many tasks on a single-threaded event loop.

Example

import asyncio

async def fetch(name: str, delay: float) -> str:
    await asyncio.sleep(delay)          # stands in for a network call
    return f"{name} done"

async def main() -> None:
    results = await asyncio.gather(fetch("a", 1), fetch("b", 1), fetch("c", 1))
    print(results)                      # finishes in ~1s, not ~3s

asyncio.run(main())

Good to know

  • It speeds up I/O-bound work (HTTP, databases, files), not CPU-bound work. For CPU-bound work use processes.
  • Never call blocking functions such as time.sleep or requests.get inside a coroutine; they freeze the whole loop.
  • asyncio.TaskGroup (Python 3.11+) is the safer modern way to run and cancel groups of tasks.

Official documentation ↗

Used in these posts

Python • 1 min read Reader level: Advanced

What you'll learn

  • How the asyncio event loop runs coroutines
  • How to use task groups and async context managers (Python 3.11+)
  • How to build a producer/consumer pipeline with asyncio.Queue
  • Common pitfalls and the real performance gains
Python Asyncio Patterns: From Callbacks to Structured Concurrency

Learn how to write high-performance async Python code using asyncio — covering coroutines, task groups, async context managers, and real-world HTTP client patterns.

#python#asyncio#concurrency#performance