If you’ve ever written Python code that feels slow when doing things like API calls or waiting for data... you’ve already felt the problem that asyncio solves.
Async basically means
“Don’t wait - do something else meanwhile”
Imagine This First
Synchronous - Everything one by one
- You order food
- You stand there waiting...
- You do nothing else
- Food arrives
- Then you order the next thing
That's how python normally works, everything works line by line
Asynchronous - No waiting
- You order food
- You sit down
- You chat, scroll your phone
- When your food is ready, you go pick it up
You didn’t waste time waiting, that's asyncio.
Problem with Normal Python
Let’s look at a simple example:
import time
def task(i):
print(f"Start {i}")
time.sleep(2)
print(f"End {i}")
task(1)
task(2)
You know the output, it'll run each task one by one
Output
Start
(wait 2 sec)
End
Start
(wait 2 sec)
End
In Asyncio - we do this efficiently
import asyncio
async def task(i):
print(f"Start {i}")
await asyncio.sleep(2)
print(f"End {i}")
async def main():
await asyncio.gather(task(1), task(2))
asyncio.run(main())
Output
Start 1
Start 2
End 1
End 2
What’s happening step by step:
1) The program starts an Event loop (A task manager) using asyncio.run(main()).
2) Inside main(), two task() Coroutines are created.
async def- This creates aCoroutine function. Think of it as a special function that can pause and continue later without blocking the whole program.
3) asyncio.gather() tells the event loop to run both at the same time. concurrently - (they take turns on a single thread; not true parallel execution by default). It waits until all of them finish.
4) Each task():
- Prints "Start"
- Pauses for 2 seconds using await
asyncio.sleep(2). A non-blocking delay. It pauses the coroutine for some time, but doesn’t stop the whole program (unlike time.sleep()). While one task is “sleeping”, the other one can run. - After 2 seconds, both resume and print "End".
When to use Asyncio ?
Multiprocessing VS Multithreading VS Asyncio
If you've previously worked python or any other language, you must have heard these terms.
Here's a quick comparison between them
| Feature | Multiprocessing | Multithreading | Asyncio |
|---|---|---|---|
| Core Idea | Multiple processes (separate memory) | Multiple threads (shared memory) | Single thread, cooperative tasks |
| True Parallelism | Yes (uses multiple CPU cores) | No (limited by GIL in Python) | No (runs on single thread) |
| Best For | CPU-bound tasks | I/O-bound tasks | I/O-bound tasks (high concurrency) |
| Memory Usage | High (separate processes) | Low (shared memory) | Very low |
| Libraries | multiprocessing | threading | asyncio, aiohttp |
Multiprocessing → Multiple chefs in separate kitchens OR Do many heavy tasks at the same time - multiple CPU cores.
Multithreading → Multiple chefs in one kitchen sharing tools OR One core rapidly switches between threads.
Async → One chef switching between dishes while waiting OR One core, one thread, manually switching tasks at
awaitpoints.
Wrapping up
As I said earlier, Asyncio is
“Don’t wait—do something else meanwhile”



Top comments (0)