Technology Async and Concurrency | Python Tutorial

Learn with diagrams, code, systems and practical examples.

</>
TutorialsPythonAsync and Concurrency
Advanced Python · Chapter 25

Async and Concurrency

Understand when async code helps and when it adds unnecessary complexity.

Why this matters

Applications often wait on network or disk operations, and concurrency can improve throughput when tasks spend significant time waiting.

Start with the idea

asyncio lets one thread make progress on other tasks while awaiting I/O. async def defines a coroutine and await pauses that coroutine until an awaitable operation can continue.

IdeaUnderstand the purpose
CodeRun the smallest example
PracticeChange it yourself
SubjectVision learning pattern: understand the idea before memorizing syntax.

Small working example

Run this example first. Do not change several things at once; confirm the basic behavior, then experiment.

Python
import asyncio

async def main():
    await asyncio.sleep(0.1)
    print("done")

asyncio.run(main())
Expected output
done
What to notice

The example is intentionally small. Focus on the chapter’s main idea before adding extra syntax or framework code.

Common beginner mistake

Mistake

Using async for CPU-heavy work and expecting it to make calculations automatically run in parallel.

Better approach

Use async primarily for I/O-bound concurrency and choose other strategies for CPU-bound workloads.

Quick recap

  • Async is mainly useful for waiting-heavy workloads.
  • await yields control cooperatively.
  • Concurrency choices should match the workload.

Try it yourself

These are deliberately small. If you can complete them without copying the example, you are ready to continue.

  1. Run two sleeping coroutines concurrently.
  2. Identify whether a sample task is I/O-bound or CPU-bound.