Multithreading and Multiprocessing
Multithreading and Multiprocessing
Multithreading and Multiprocessing
MULTIPROCESSING
The ability of a processor to execute several unrelated processes
simultaneously is known as multiprocessing. These processes do not
share any resources.
Multiprocessing breaks down processes into smaller routines that run
independently. The more tasks a single processor is burdened with, the
more difficult it becomes for the processor to keep track of them.
It evidently gives rise to the need for multiprocessing. Multiprocessing
tries to ensure that every process gets its own processor/processor core
and that execution is hassle-free.
In the case of multicore processor systems like Intel i3, a processor core
is allotted to a process.
USAGE OF MULTITHREADING
Use Case: Multithreading is suitable when the program involves tasks
that are I/O-bound or tasks that spend a lot of time waiting for external
resources, such as reading and writing files, network operations, or user
input.
Global Interpreter Lock (GIL): Python has a Global Interpreter Lock (GIL),
which means only one thread can execute Python bytecode at a time in a
single process. Therefore, multithreading may not be as effective for
CPU-bound tasks that require significant computational power, as the GIL
can limit parallelism.
Example: Web scraping, downloading files, or handling multiple user
requests simultaneously in a web server.
USAGE OF MULTIPROCESSING
Use Case: Multiprocessing is suitable for CPU-bound tasks or tasks that
require significant computational power, as it allows for parallel
execution of code in separate processes. Each process has its own
Python interpreter and memory space, avoiding the GIL limitation.
Example: CPU-intensive computations, data processing, image
processing, or any task that can benefit from parallelism.
REFERENCES
https://www.scaler.com/topics/multithreading-in-python/
https://www.youtube.com/watch?v=ICbU6zAKtqQ&t=785s
https://www.youtube.com/watch?v=zGe-9LfnAaA