Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 40 ยท Getting Started

What Is asyncio and Why Use It?

Learn what Python's asyncio does, how cooperative concurrency differs from threads and processes, and when waiting-heavy programs benefit from async code.

Most programs spend a surprising amount of time waiting: for a web server to answer, for a database query to finish, for a file to be read, for a timer to fire. While your code waits, the CPU sits idle. asyncio is the part of Python's standard library that lets one program use that idle time to do other useful work, without starting extra threads or processes.

In this course you will learn asyncio from the ground up: coroutines, the event loop, tasks, synchronization, networking patterns, cancellation, testing and finally complete projects. This first lesson gives you the mental model. If you are new to Python itself, the Python tutorial covers the language basics you will need.

About the examples. Every runnable example on this site runs in your browser, inside Python 3.14 compiled to WebAssembly. Real sockets, threads and subprocesses do not exist there, so lessons that need them show read-only code and use small in-memory stand-ins that are labeled as such. Everything about coroutines, tasks, timers, locks and queues is the real asyncio.

Waiting is the bottleneck

Imagine you must fetch ten web pages and each answer takes one second. A normal (synchronous) program asks for page one, waits one second, asks for page two, waits another second, and so on. Total time: about ten seconds, nearly all of it spent doing nothing.

An asynchronous program sends all ten requests, then waits for the answers together. Total time: about one second. The pages arrive at the same moment either way. What changed is that the program no longer blocks while it waits.

Synchronous: one thing at a time

Ask for page 1 Then wait
Idle CPU Nothing else runs
Ask for page 2 Only after page 1

Total time is the sum of every wait

1 / 2

Concurrency is not parallelism

These two words are often confused:

  • Concurrency means several tasks are in progress during the same period. They may take turns on a single CPU core.
  • Parallelism means several tasks are literally running at the same instant, on different cores.

asyncio gives you concurrency on a single thread. Only one piece of your code runs at any moment, but whenever it reaches a point where it must wait, it hands control to another piece. This style is called cooperative multitasking, because every task must politely say "I am waiting now, someone else may go" using the await keyword. Nothing interrupts a task from the outside.

Python offers three ways to do several things at once:

Tool How it works Best for
asyncio One thread, tasks switch at await Many waiting-heavy (I/O-bound) jobs: network, timers, queues
threading Several OS threads, switched by the OS I/O-bound jobs using blocking libraries
multiprocessing Several processes, true parallelism CPU-bound work such as number crunching

Because asyncio runs on one thread, you do not need locks around ordinary variables: nothing can interrupt you between two lines that contain no await. This removes a whole family of thread bugs, and you will see exactly how it works in the event loop lesson.

A first taste

Here is the smallest useful comparison. Three "jobs" each wait a fraction of a second. First the blocking way, then the asynchronous way. Run it and compare the two times.

Python Blocking versus async waiting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The blocking version takes about 0.6 seconds (three waits added up). The async version takes about 0.2 seconds, because all three waits overlap. You do not need to understand gather yet; the point is the shape of the result.

What asyncio is good at

asyncio shines when a program has lots of waiting and little computing:

  • web servers and API clients that talk to many other services
  • chat servers, bots and WebSocket applications
  • web crawlers and download tools
  • programs that talk to databases through async drivers
  • anything with many timers, queues or long-lived connections

It does not speed up pure computation. A loop that adds a billion numbers runs at the same speed inside async def as outside it, and while it runs nothing else can progress. For heavy computing use processes, or hand the work to a worker as you will see in asyncio versus threads and processes.

The vocabulary you will meet

Here is a map of the whole course so the later terms feel familiar:

  • Coroutine: a function defined with async def; calling it produces a pausable job (first coroutine).
  • Event loop: the scheduler that runs coroutines and wakes them when what they wait for is ready (event loop).
  • Task: a coroutine wrapped so it runs in the background (tasks).
  • Future: a placeholder for a result that arrives later (futures).
  • Awaitable: anything you can put after await.
flowchart LR
    C["Coroutine<br/>async def"] --> T["Task<br/>scheduled job"]
    T --> L["Event loop<br/>runs tasks"]
    L --> W["Waits on I/O,<br/>timers, queues"]
    W --> L

Coroutines become tasks, the loop runs them, and every wait hands control back to the loop.

A second example: a visible timeline

To see switching happen, print a message each time a job starts and finishes. The messages interleave, proving that the jobs overlap:

Python Watching jobs interleave

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

All three start immediately. They finish in order of their waiting time, not in the order you listed them: photo.png, then video.mp4, then report.pdf.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does asyncio let a single thread do while one job is waiting?

    At every await the running job hands control back so another ready job can run. Nothing runs in parallel and nothing is interrupted.
  2. 2. Which workload benefits LEAST from asyncio?

    Pure computation never waits, so there is nothing to overlap. CPU-bound work needs processes or optimized libraries.
  3. 3. Why is asyncio called cooperative multitasking?

    Only code that reaches an await point lets other tasks run. If a task never awaits, everything else is stuck.
  4. 4. Three jobs each wait 0.2 seconds. Roughly how long do they take when run together with asyncio?

    The waits overlap, so the total is close to the longest single wait.

Summary

  • asyncio runs many waiting-heavy jobs on one thread by switching at await points.
  • It provides concurrency, not parallelism, and does not speed up CPU-bound code.
  • The key words: coroutine, event loop, task, future, awaitable.
  • Blocking calls such as time.sleep() stop the whole program; await asyncio.sleep() does not.

Next: Your first coroutine