Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 40 ยท Getting Started

asyncio vs Threads vs Processes

Compare asyncio, threads and processes in Python. Learn how the GIL affects each, which fits I/O-bound or CPU-bound work, and how to mix them safely.

Python gives you three main tools for doing several things at once. They look similar from far away, but they solve different problems, and picking the wrong one is the most common reason concurrent code ends up slower or buggier than the plain version. This lesson helps you choose, and shows how asyncio cooperates with the other two when a program needs both.

I/O-bound and CPU-bound work

Every slow program is slow for one of two reasons:

  • I/O-bound: it waits for something outside the CPU, such as a network reply, a disk read or a timer. The CPU is mostly idle.
  • CPU-bound: it computes all the time, such as resizing images, parsing huge files or running simulations. The CPU is the bottleneck.

Overlapping waits helps I/O-bound programs enormously. Overlapping computations only helps if they run on different CPU cores.

The GIL in one paragraph

CPython has a Global Interpreter Lock (GIL): in the standard build, only one thread executes Python bytecode at a time. Threads therefore cannot speed up pure Python computing, but they release the lock while waiting for I/O, so they still help with waiting. Processes each have their own interpreter and their own GIL, so they give real parallelism. (Python 3.13 and newer also offer an optional free-threaded build without the GIL; the trade-offs below still apply to the standard build most people run.)

The three tools compared

asyncio threading multiprocessing
Units of work coroutines (tiny objects) OS threads OS processes
Switching explicit, at await preemptive, any time preemptive, any time
Parallel Python code no no (GIL) yes
Memory per unit a few KB about 8 MB of stack reserved a whole interpreter
Practical limit tens of thousands hundreds about the CPU core count
Shared state easy, no data races between awaits needs locks separate memory, needs queues/pipes
Needs async-aware libraries yes no no
flowchart TD
    Q["What is slow?"] --> IO{"Waiting<br/>for I/O?"}
    IO -->|"yes, many connections"| A["asyncio"]
    IO -->|"yes, but blocking libraries"| T["threads"]
    Q --> CPU{"Computing?"}
    CPU -->|"yes"| P["processes or<br/>faster libraries"]

A quick decision guide: waiting points to asyncio or threads, computing points to processes.

Cheap tasks: why asyncio scales

A thread reserves stack memory and the operating system must schedule it, so a few thousand threads strain a machine. A coroutine is an ordinary Python object. This example starts 10,000 coroutines that each wait briefly. It finishes almost instantly, which would be impractical with 10,000 threads:

Python Ten thousand waiting tasks

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 10,000 jobs sleep at the same time on one thread. That is the strength of asyncio for servers with many connections.

The weakness: blocking the loop

All coroutines share one thread. If one of them runs a long computation or calls a blocking function, nobody else can run until it returns. The next example runs a ticker that should beat every 10 ms, then does 0.2 seconds of busy computing:

Python CPU work starves the loop

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.

During heavy() the ticker got zero turns, even though 0.2 seconds is long enough for twenty ticks. The loop was stuck inside a normal function call. In a web server, that means every connected user freezes. The remedies are covered in blocking the event loop.

Mixing: run blocking work off the loop

The standard solution is to keep the event loop for coordination and send blocking or CPU-heavy work to a pool:

  • await asyncio.to_thread(func, *args) runs func in a worker thread. Best for blocking I/O libraries (a legacy database driver, requests, file operations) and for work that releases the GIL.
  • await loop.run_in_executor(pool, func, *args) does the same with a pool you choose. Passing a ProcessPoolExecutor gives real CPU parallelism.

Real code looks like this (read-only because the editor on this site has no operating-system threads or processes):

Python
import asyncio
from concurrent.futures import ProcessPoolExecutor


def crunch(n: int) -> int:
    return sum(i * i for i in range(n))


async def main():
    loop = asyncio.get_running_loop()
    with ProcessPoolExecutor() as pool:
        results = await asyncio.gather(
            loop.run_in_executor(pool, crunch, 2_000_000),
            loop.run_in_executor(pool, crunch, 3_000_000),
        )
    print(results)


if __name__ == "__main__":
    asyncio.run(main())

Both crunch calls run in separate processes, in parallel, while the loop stays free. On this site the editor emulates to_thread and run_in_executor by running the function inline, so the code works but is not parallel. The call shape is what matters:

Python to_thread keeps the same call shape

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.

On a normal computer the two lookups run in two worker threads and the loop remains responsive in the meantime.

Which should you choose?

  • Many network connections or timers, with async libraries available: asyncio.
  • A few blocking libraries, simple scripts, or code you cannot rewrite: threads (or asyncio.to_thread from inside an async app).
  • Heavy number crunching or data processing: processes, or libraries such as NumPy that run native code outside the GIL.
  • Both: an asyncio application that offloads CPU work to a process pool.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which workload is I/O-bound?

    Downloading spends most of its time waiting for the network, which is exactly what asyncio can overlap.
  2. 2. Why can threads not speed up pure Python number crunching in the standard CPython build?

    The Global Interpreter Lock serializes bytecode execution. Processes avoid it by having separate interpreters.
  3. 3. A coroutine runs a 2 second loop of pure computation without any await. What happens to other tasks?

    There is only one thread. Without an await the loop cannot switch tasks.
  4. 4. How do you run a blocking function from a coroutine without freezing the loop?

    to_thread (or run_in_executor) runs the function in a worker so the loop stays free.

Summary

  • asyncio, threads and processes solve different problems: cheap waiting, blocking libraries, and parallel computing.
  • Coroutines are tiny, so asyncio handles tens of thousands of waiting jobs on one thread.
  • One blocking or CPU-heavy call stalls every task on the loop.
  • Use to_thread or run_in_executor (with a process pool for CPU work) to keep the loop responsive.

Next: How the event loop works