Introduction
Python has three ways to do things concurrently: threading, multiprocessing, and asyncio. Most beginners reach for threads; most production code in 2026 uses async/await for I/O-bound work.
This post explains asyncio from scratch — what it is, when it helps, and the mistakes everyone makes.
The core idea
Asyncio is single-threaded cooperative multitasking. Instead of the OS switching between threads, your code decides when to pause (at await) and let other tasks run.
import asyncio
async def fetch(url):
await asyncio.sleep(1) # simulate network I/O
return f"data from {url}"
async def main():
results = await asyncio.gather(
fetch("a.com"),
fetch("b.com"),
fetch("c.com"),
)
print(results)
asyncio.run(main())
# Total: ~1s (not 3s) — all three ran concurrently
Three 1-second tasks finish in ~1 second because they overlap during the sleep (I/O wait).
When async helps (and when it doesn’t)
| Workload | Async helps? | Why |
|---|---|---|
| HTTP requests (APIs) | ✅ Yes | Waiting on network |
| DB queries | ✅ Yes | Waiting on disk/network |
| File I/O | ⚠️ Sometimes | OS-level; use aiofiles |
| CPU-bound (math, ML) | ❌ No | Async can’t parallelize CPU |
| Heavy computation | ❌ No | Use multiprocessing |
Rule: async is for I/O-bound concurrency. For CPU-bound work, use multiprocessing or a thread pool.
async / await basics
import asyncio
async def greet(name):
await asyncio.sleep(1)
print(f"Hello, {name}")
asyncio.run(greet("Galvan")) # runs the coroutine
async defdefines a coroutine (a function that can pause).awaitpauses the coroutine until the awaited thing completes.asyncio.run()is the entry point — call it once, at the top level.
Running multiple tasks
async def main():
# Sequential (slow): 3s
await fetch("a.com")
await fetch("b.com")
await fetch("c.com")
# Concurrent (fast): 1s
await asyncio.gather(fetch("a.com"), fetch("b.com"), fetch("c.com"))
# With timeouts
try:
await asyncio.wait_for(fetch("slow.com"), timeout=2.0)
except asyncio.TimeoutError:
print("Timed out")
gather runs all concurrently and returns results in order. wait_for adds a timeout.
Real example: fetch multiple URLs
import asyncio, aiohttp
async def fetch(session, url):
async with session.get(url) as resp:
return await resp.text()
async def main(urls):
async with aiohttp.ClientSession() as session:
tasks = [fetch(session, u) for u in urls]
return await asyncio.gather(*tasks)
urls = [f"https://httpbin.org/delay/1" for _ in range(5)]
results = asyncio.run(main(urls))
print(f"Fetched {len(results)} pages in ~1s")
5 pages in ~1s instead of 5s. That’s the async win.
Common patterns
Task groups (Python 3.11+)
async with asyncio.TaskGroup() as tg:
t1 = tg.create_task(fetch("a.com"))
t2 = tg.create_task(fetch("b.com"))
# both done here; exceptions propagate cleanly
Better than gather for error handling.
Semaphores (limit concurrency)
sem = asyncio.Semaphore(5) # max 5 concurrent
async def fetch_limited(url):
async with sem:
return await fetch(url)
Use when hitting rate-limited APIs — you don’t want 1000 concurrent requests.
Background tasks
async def main():
task = asyncio.create_task(background_work())
await do_other_stuff()
await task # wait for background to finish
Sync vs async libraries
Not all libraries support async. Check before using:
| Sync (blocks) | Async (non-blocking) |
|---|---|
requests |
aiohttp, httpx |
sqlite3 |
aiosqlite |
psycopg2 |
asyncpg |
time.sleep |
asyncio.sleep |
| Standard file I/O | aiofiles |
Mixing sync calls in async code blocks the event loop — the #1 beginner mistake. If you must call a sync function, use asyncio.to_thread():
result = await asyncio.to_thread(blocking_function, arg)
Common mistakes
- Calling sync code in async functions — blocks the whole event loop. Use
to_threador async libs. - Forgetting
await—fetch(url)returns a coroutine object, never runs. Alwaysawait. asyncio.run()inside a running loop — error. Useawaitinstead.- Using async for CPU work — no speedup; use multiprocessing.
- Too many concurrent tasks — thousands of coroutines can overwhelm. Use semaphores.
Key Concepts
- Coroutine =
async deffunction (pausable) await= pause here until done- Event loop = the scheduler running coroutines
gather= run many concurrently- I/O-bound = waiting on network/disk (async helps)
- CPU-bound = computation (async doesn’t help)
What to Try Next
- Convert your web scraper to async with
aiohttp. - Use
asyncioin a FastAPI backend — FastAPI is async-native. - Add async DB calls with
asyncpgto your full-stack app. - Combine with type hints —
async def fetch() -> str.
FAQ
Asyncio vs threading vs multiprocessing?
- Threading: OS switches threads; good for I/O, limited by GIL for CPU.
- Multiprocessing: separate processes, true CPU parallelism.
- Asyncio: single thread, cooperative; best for high-concurrency I/O (thousands of connections).
For 10k concurrent HTTP requests, asyncio wins. For CPU work, multiprocessing wins.
Is async Python faster than sync?
For I/O-bound work with many concurrent operations: yes, dramatically. For single sequential operations: no, async adds overhead. Don’t async a single requests.get.
Does Streamlit support async?
Streamlit is sync. You can call async code with asyncio.run() inside a Streamlit app, but it blocks the UI during execution. For async backends, put the async code in a FastAPI service and call it from Streamlit.