Sitemap
Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Member-only story

async Won't Make Your Python Code Faster (and When It Will)

8 min read5 days ago

--

There’s a pattern I see again and again. A service feels slow, someone says “we should use async”, and a week later half the codebase is full of async def and await. The code is harder to read, the stack traces are stranger, and the latency graph looks exactly the same.

Press enter or click to view image in full size
Image created by author

That’s not because async is bad. It’s because async solves one specific problem, and most slow code has a different one.

So instead of arguing about it, I measured. Four experiments, about 60 lines of code, and a clear answer to the question every Python developer eventually asks: will async make my code faster?

Short answer: only if your code spends most of its time waiting.

Let me show you what that means.

An async program runs on one thread. There is an event loop, and it juggles many tasks. When a task hits an await on something slow, like a network call, it says "wake me when this finishes" and the loop switches to another task.

--

--

Kiran Grewal
Kiran Grewal

Written by Kiran Grewal

✦ web developer ✦ MCA in web development ✦ Love to talk about Technology, AI and Programming tips and tricks

Python in Plain English
Python in Plain English

Published in Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.