In previous post, I explained the difference between concurrency and parallelism.
A short recap:
Parallelism:Doing multiple tasks at exactly the same time; only possible in multi-core (two or more processors) CPUs where each core is assigned a task.
Concurrency:Dealing with multiple tasks by switching context between them.
Before diving into full explanation of Multi-threading and Multiprocessing, it is worth noting that both of them can be utilized to improve the performance of an application when used correctly.
Process:A computer process can be thought of as an entire isolated section where different tasks execution occur. A process does not directly interfere with another process.
Thread: it refers to the smallest unit of processing that can be performed in an Operating System. It can be seen as a light-weight process or a subset of a process
Going with the definitions above, Multi-threading is when multiple threads exist within one process.
Say you have a single core CPU and you need to execute three different Tasks(A, B, and C.). You can spin up multiple threads programmatically (i.e. 3 in this case) where each of the threads is allowed to execute a task. These tasks are executed concurrently(i.e. switching between the three tasks). This context switching between the three tasks is so fast because it is handled by the process. It switches from one task to another whenever one of the tasks is idle(i.e. waiting for something else)
Multiprocessing is when two or more processes are executing tasks simultaneously. Most modern CPUs have multiple cores possible and this makes it possible to run multiple processes in parallel.
Python, by design, only allows one thread to run at a time even in multi-core CPUs. This means that multi-threaded code in python doesn’t behave the way you would expect when compared with other languages like Java.
However multi-threading in Python can still be used to reduce execution time by using it for I/O-bound operations (i.e. database or network request)because of context switching when a task is idle while Multiprocessing is used for CPU-bound operations(i.e. big mathematical computations)
- Multi-threading on a single core is concurrency(context switching between threads)
- Multi threading on a multi-core CPUs can be parallelism (different threads on different processes running simultaneously)
Enjoyed this article?