Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Concurrency vs Parallelism vs Asynchrony | The Asyncio Foundation
Python Asyncio in Depth

Concurrency vs Parallelism vs Asynchrony

Scorri per mostrare il menu

These three terms are often used interchangeably, but they describe different things. Mixing them up leads to wrong tool choices and hard-to-debug code.

Concurrency

Concurrency means dealing with multiple tasks at once – but not necessarily running them at the same time.

Think of a chef preparing a meal. While the pasta boils, they chop vegetables. They are not doing two things simultaneously – they switch between tasks whenever one requires waiting. One person, multiple tasks in progress.

In Python, concurrency means the program can start a new task before the previous one finishes. Asyncio is a concurrency tool.

Parallelism

Parallelism means tasks are literally running at the same time – on multiple CPU cores simultaneously.

Two chefs, two stoves. Real simultaneous execution.

In Python, true parallelism requires multiple processes (via the multiprocessing module), because the GIL (Global Interpreter Lock) prevents multiple threads from executing Python bytecode at the same time.

1234567891011
# Parallelism: CPU-bound tasks running on separate cores from multiprocessing import Pool # Calculating squares for large numbers def compute_square(number): return number ** 2 with Pool(processes=2) as pool: results = pool.map(compute_square, [1000000, 2000000, 3000000]) print(results)

Asynchrony

Asynchrony describes the programming model – code that doesn't block while waiting. An async function starts an operation and says: "notify me when this is done, I'll do other things in the meantime."

Asyncio implements asynchrony using a single thread. There is no parallelism involved – just smarter scheduling of when to wait and when to work.

The key insight: asyncio is not for making code faster in general – it's for eliminating idle waiting time during I/O.

question mark

You need to fetch data from 50 external APIs simultaneously. Which tool is the best fit?

Seleziona la risposta corretta

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 2

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Sezione 1. Capitolo 2
some-alt