Course contentsShow
← Python
Lesson 855 of 1,06014. Concurrency and PerformanceFree lesson

Distinguishing Concurrency From Parallelism

You will identify the conceptual difference between handling multiple tasks simultaneously versus managing multiple tasks by switching between them.

Distinguishing Concurrency From Parallelism

In this lesson, you will learn how to differentiate between the structural design of task management (concurrency) and the physical execution of computational work (parallelism).

The Conceptual Divide

At the advanced level, it is vital to stop viewing these terms as synonyms. Concurrency is about structure; it is the ability to deal with multiple things at once by switching between them. If you are a chef in a kitchen, you are being concurrent when you chop onions, stir a simmering sauce, and check the oven, moving your focus from one task to another as each waits for a moment of attention. You are only one person, but you are managing multiple "live" tasks.

Parallelism, conversely, is about execution; it is the ability to perform multiple things at the exact same time. Parallelism requires multiple workers—in computing terms, multiple CPU cores. If you hire a second chef to work alongside you, now the sauce and the onions can truly be processed simultaneously.

Illustration in Python

In Python, we often use asyncio for concurrency. When you await an I/O operation, you are yielding control, allowing the event loop to switch to another pending task. No extra CPU work is being done; you are simply managing the wait times more efficiently.

import asyncio

async def fetch_data(task_id):
    print(f"Task {task_id} starting")
    await asyncio.sleep(1)  # Concurrent: pausing here allows other tasks to run
    print(f"Task {task_id} finished")

# We manage multiple tasks concurrently, but they share the same execution thread.
asyncio.run(asyncio.gather(fetch_data(1), fetch_data(2)))

When we move to CPU-bound work (like heavy data transformation or numerical analysis), concurrency often fails to provide a speedup because the Global Interpreter Lock (GIL) restricts execution to one thread. To achieve true parallelism, we must bypass the GIL by using the multiprocessing module to spawn separate memory spaces and CPU-bound processes.

Key Takeaway: Concurrency is about managing many tasks at once by interleaving their progress, while parallelism is about executing multiple tasks at the exact same physical moment.