Sponsored Reserved space — layout preview until AdSense is connected

Tech

async / await in Python — a practical first look

Understand coroutines, the event loop, and when asyncio beats threads for I/O-heavy programs.

May 2026 · 9 min read · 1 views · 0 hearts

asyncio lets a single thread juggle thousands of waiting operations by switching between them whenever one pauses for I/O.

Coroutines

A function defined with async def is a coroutine; await hands control back to the event loop while waiting:

import asyncio

async def fetch(n):
    await asyncio.sleep(1)
    return n * 2

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

asyncio.run(main())

When to use it

Async wins for many concurrent I/O waits — web scraping, API gateways, chat servers. For CPU-bound work it does not help; use processes instead.

The catch

Async is contagious: calling async code generally means your callers become async too. Decide early whether a project needs it before threading it through everything.

Comments

Questions, corrections, and tips stay visible for everyone reading this page.

0 in thread

Join the discussion

Shown next to your comment.

Constructive tone · up to 4,000 characters

No comments yet

Be the first to leave a note — it helps the next reader.

Sponsored

Sponsored Reserved space — layout preview until AdSense is connected