Coroutines and Concurrency

Some tasks have to be executed sequentially, some may be performed in parallel, and yet others may be done concurrently. Most people put on the underwear before putting on the pants, though the order may be in reverse—even then, superwoman and superman don’t put them on at the same time. These two tasks are inherently sequential; don’t attempt an alternative.

Sequential execution is clear; we perform one task to completion before beginning the next. But some confusion exists among programmers between parallel and concurrent execution. Understanding the difference between them is critical, as multithreading on a multi-core processor generally is parallel, while coroutines are generally used more often for concurrent execution rather than parallel execution.

Parallel vs. concurrent

Let’s understand the difference between parallel and concurrent using an example. Suppose longtime friends Sara and Priya meet for dinner, and Sara is curious about Priya’s recent international trip. Priya can listen to Sara while munching on food, but she’ll never speak with her mouth full, as you can see in the following figure.

Get hands-on with 1200+ tech skills courses.