Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Concurrency vs Parallelism vs Asynchrony | The Asyncio Foundation
Python Asyncio in Depth

Concurrency vs Parallelism vs Asynchrony

Свайпніть щоб показати меню

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?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Секція 1. Розділ 2
some-alt