No history yet

Introduction to Concurrency

Juggling Tasks

Imagine a chef in a kitchen making a meal. They need to chop vegetables, boil water for pasta, and simmer a sauce. The chef starts by putting a pot of water on the stove. While it heats up, they don't just stand there and watch it. Instead, they start chopping vegetables. Then, they might give the sauce a quick stir before going back to chopping. When the water boils, they add the pasta.

This is the essence of concurrency. It's the art of managing multiple tasks at once, making progress on all of them over a period of time. The chef has three tasks—boiling water, chopping, and simmering sauce—all in progress simultaneously. They are structuring their work by switching between tasks at sensible moments.

Concurrency is about dealing with lots of things at once. It's a way to structure a program by breaking it into tasks that can be managed independently.

In modern software, this is crucial. A web server handles requests from thousands of users. A mobile app needs to download data from the internet while keeping its user interface smooth and responsive. Concurrency makes this possible by allowing a program to work on a new task before a previous one has finished.

Concurrency vs. Parallelism

People often use the terms "concurrency" and "parallelism" interchangeably, but they describe two different ideas. The distinction is important.

Concurrency is about the structure of the tasks. It’s the act of juggling—switching between different tasks. Our solo chef is concurrent.

Parallelism is about the execution of the tasks. It’s about doing multiple things at the exact same time. To achieve parallelism in the kitchen, you'd need more than one chef. One chef could chop vegetables while another chef stirs the sauce. This requires multiple workers—or in computing, multiple CPU cores.

You can have concurrency on a machine with a single CPU core, like our solo chef. But you need multiple CPU cores to achieve true parallelism. In short, parallelism is one way to implement concurrency, but it's not the only way.

Why Is Concurrency Important?

Concurrency solves two major problems in software: responsiveness and efficiency.

Think about a desktop application with a button that saves a large file. If the program isn't concurrent, clicking that button would freeze the entire application until the save is complete. You wouldn't be able to click anything else. With concurrency, the save operation can run as a background task, and the user interface remains responsive.

Concurrency also improves efficiency, especially for programs that spend a lot of time waiting. These are called I/O-bound tasks. I/O stands for input/output, and it includes things like waiting for a response from a web server, reading data from a database, or accessing a file on a hard drive. While a program is waiting for an I/O operation to complete, the CPU has nothing to do. Concurrency allows the CPU to switch to another task during these idle periods, making much better use of its time.

Lesson image

Different programming languages provide different models for writing concurrent code. Two of the most common are:

  1. Threading: This model involves creating multiple threads of execution within a single program. You can think of threads as lightweight workers that share the program's memory. This makes it easy for them to share information, but it also introduces complexity. If multiple threads try to modify the same data at the same time, they can interfere with each other, leading to bugs that are difficult to track down.

  2. Asynchronous Programming: This is a different style where tasks are designed to be cooperative. A task runs until it hits a point where it has to wait for something (like a network request). At that point, it explicitly hands over control to another task. This avoids many of the complexities of threads but requires a different way of thinking about program flow.

Understanding these fundamental concepts is the first step. Now you're ready to see how a language like Python puts these ideas into practice.

Quiz Questions 1/5

Which of the following scenarios best illustrates the concept of concurrency without parallelism?

Quiz Questions 2/5

True or False: A program can be concurrent on a computer with only a single CPU core.