Technology May 02, 2026 · 3 min read

Asyncio - in Python Explained Like You’re Ordering Food (A Beginner Guide)

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 o...

DE
DEV Community
by Anant Mishra
Asyncio - in Python Explained Like You’re Ordering Food (A Beginner Guide)

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

Async_vs_sync

Synchronous - Everything one by one

  1. You order food
  2. You stand there waiting...
  3. You do nothing else
  4. Food arrives
  5. 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

event_loop_async

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 a Coroutine 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 ?

Asyncio usage

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 await points.

Wrapping up

As I said earlier, Asyncio is

“Don’t wait—do something else meanwhile”

DE
Source

This article was originally published by DEV Community and written by Anant Mishra.

Read original article on DEV Community
Back to Discover

Reading List