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

Concurrency vs Parallelism vs Asynchrony

Deslize para mostrar o 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?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 2

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Seção 1. Capítulo 2
some-alt