Something that always gets us confused. Concurrency vs. Parallelism. Let’s debunk it once and for all ⚡
Concurrency: When two or more tasks run during the overlapping time and make some progress is Concurrency.
Parallelism: When two or more tasks run during the overlapping time and make some progress simultaneously is Parallelism.
The only difference between the two statements is the word “simultaneous,” making all the difference.
Two tasks running concurrently can optionally use choose to execute on two separate cores of the CPU or execute on a single core and context switch to make progress.
But when two tasks are running in parallel, they have to be making progress simultaneously at a given instant. This means we can say …
⚡ all parallel executions are concurrent, but all concurrent executions need not parallel
Unlike popular belief, Concurrency and Parallelism are not mutually exclusive definitions; rather, they are nested. 🔨
Let’s talk Python and see where concurrency and parallelism fit in.
In Python, you can get concurrency using threads and asyncio. But if you need parallelism, you will have to use multiprocessing, forking out multiple processes to perform tasks in parallel.
This is typically due to a notorious feature called Global Interpreter Lock, fondly called GIL, which intentionally allows only one thread to hold the control of the Python interpreter at any given instant, preventing the execution and scheduling of others.
⚡ If you seek to become a better engineer and learn System Design the right way, you should definitely enroll in my upcoming cohort starting August 5th.
You can find the week-by-week curriculum and topics, benefits, testimonials, and other details arpitbhayani.me/course.