Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

MULTITHREADING

 Multithreading is defined as the ability of a processor to execute multiple


threads concurrently.
 Multithreading in Python is a popular technique that enables multiple
tasks to be executed simultaneously. In simple words, the ability of a
processor to execute multiple threads simultaneously is known as
multithreading.
 Python multithreading facilitates sharing data space and resources of
multiple threads with the main thread. It allows efficient and easy
communication between the threads.
 If we wish to save time and improve performance, we should use
multithreading in Python.

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.

DIFFERENCE BETWEEN MULTITHREADING & MULTIPROCESSING


MULTITHREADING MULTIPROCESSING
It is a technique where a process It is a technique where multiple
spawns multiple threads processes run across multiple
simultaneously. processors/processor cores
simultaneously.
Python multithreading implements Python multiprocessing implements
concurrency. parallelism in its truest form.
It gives the illusion that they are It is parallel in the sense that the
running parallelly, but they work in a multiprocessing module facilitates the
concurrent manner. running of independent processes
parallelly by using subprocesses.
In multithreading, the GIL or Global In multiprocessing, each process has its
Interpreter Lock prevents the threads own Python Interpreter performing the
from running simultaneously. execution.

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

You might also like