Skip to content
tezvyn:

Python's async/await: Concurrent, Not Parallel

Source: docs.python.orgMediumHow cards are made

Python's async/await: Concurrent, Not Parallel

async/await lets a single Python thread juggle multiple tasks, pausing one to work on another while it waits for I/O. It's ideal for network requests or database queries. The footgun: it won't speed up CPU-bound tasks, it only helps with waiting.

Why it exists

Traditional programs execute one instruction at a time. When a program waits for a network response or a file to be read from a disk (I/O operations), the CPU sits idle. Asynchronous programming was created to use that idle time to do other useful work instead of blocking the entire application.

The mental model

Think of a chef in a kitchen. A synchronous chef would put a dish in the oven, stare at it for 30 minutes, then take it out and start chopping vegetables. An asynchronous chef puts the dish in the oven, sets a timer (await), and immediately starts chopping vegetables. When the timer dings, they pause chopping, handle the oven, and then resume. The async and await keywords provide a clean way to define these pausable functions (coroutines) and the points where they should yield control.

How it works

The async def keyword defines a function as a "coroutine," a special function that can be paused and resumed. The await keyword is used inside a coroutine to signal a pause point. When Python encounters await, it tells the "event loop" (the coordinator, provided by a library like asyncio) that it's waiting for something. The event loop can then run other ready tasks. Once the awaited operation is complete, the event loop resumes the paused coroutine from where it left off. This all happens on a single thread, avoiding the complexity of multi-threading for I/O-bound tasks.

When to use it

Use async/await for I/O-bound operations. This is any task where your code spends most of its time waiting for an external resource. Great examples include building high-performance web servers that handle many simultaneous connections (like with FastAPI or aiohttp), writing clients that make many API calls concurrently, or managing database connections that have network latency.

When not to use it

Do not use async/await for CPU-bound work. This includes tasks like complex mathematical calculations, image processing, or data compression. Since asyncio runs on a single thread, a long-running CPU task will block the entire event loop, preventing any other tasks from running and defeating the purpose of concurrency. For CPU-bound work, use Python's multiprocessing module instead.

One canonical example

This program uses asyncio.sleep to simulate waiting for a slow process without blocking. The await keyword pauses the function, allowing the event loop to work on other tasks if any were available. After the sleep duration, the function resumes.

import asyncio
async def main():
print('Hello ...')
await asyncio.sleep(1)
print('... World!')
asyncio.run(main())

Interview question

For which scenario would Python's async/await typically NOT provide a performance benefit?

  • a.Reading data from several different database tables.
  • b.Processing a large dataset with intensive numerical calculations.Correct
  • c.Making multiple concurrent API calls to different services.
  • d.Handling many simultaneous incoming web requests in a server.
Why?

Async/await is designed for I/O-bound tasks where the program spends time waiting, allowing other tasks to run during these waits. CPU-bound tasks, like intensive numerical calculations, will block the single event loop, preventing any other tasks from progressing and thus negating the benefits of concurrency.

Just read this? Test yourself on what you have been reading.

Read the original → docs.python.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.

See open roles