0% found this document useful (0 votes)
32 views

Python Thread&Processing

Threads allow tasks to run concurrently within a process by sharing memory space. Processes run independently with separate memory spaces, allowing true parallelism. Threads are suitable for I/O-bound tasks due to releasing the GIL during I/O, while processes are better for CPU-bound tasks as they bypass the GIL. Both provide concurrency, but processes provide isolation and true parallelism between tasks.

Uploaded by

Shantha Kumar S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Python Thread&Processing

Threads allow tasks to run concurrently within a process by sharing memory space. Processes run independently with separate memory spaces, allowing true parallelism. Threads are suitable for I/O-bound tasks due to releasing the GIL during I/O, while processes are better for CPU-bound tasks as they bypass the GIL. Both provide concurrency, but processes provide isolation and true parallelism between tasks.

Uploaded by

Shantha Kumar S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

#PYTHONTIPS

@VEDANTSOLANKI
#PYTHONTIPS

A thread is like a separate worker


that performs a specific task within
a larger process.
Threads allow multiple tasks to be
executed concurrently within a
process, sharing the same memory
space.

@VEDANTSOLANKI
#PYTHONTIPS

Let's understand thread better with


the help of an analogy , Team
Members in a Project
Imagine you're working on a
project with a team. Each team
member focuses on a specific task,
and all team members work
together simultaneously to
accomplish the project goals.
This parallel execution resembles
how threads operate within a
process.

@VEDANTSOLANKI
#PYTHONTIPS

A process is like an independent


instance of a program that runs on
the operating system.
It has its own memory space and
resources. Processes enable
multiple programs or tasks to run
concurrently and independently of
each other.

@VEDANTSOLANKI
#PYTHONTIPS

Let's understand process better with


the help of an analogy , Separate
Companies
Think of processes as separate
companies working on different
projects. Each company has its own
employees, resources, and
workspace. They can work
independently without directly
affecting each other.
This represents how processes
operate independently in the
operating system.

@VEDANTSOLANKI
#PYTHONTIPS

Threads in Python are managed by


the interpreter's thread module.
The Global Interpreter Lock (GIL)
allows only one thread to execute
Python bytecode at a time.
This means that although threads
can run concurrently, they do not
achieve true parallelism for CPU-
bound tasks.
However, they can provide
concurrency for I/O-bound tasks
since the GIL is released during
I/O operations.

@VEDANTSOLANKI
#PYTHONTIPS

Threads share the same memory


space (heap) within a process, which
can lead to potential data race
conditions if proper
synchronization mechanisms are
not used.

@VEDANTSOLANKI
#PYTHONTIPS

Processes in Python are managed by


the multiprocessing module.
Unlike threads, processes run in
separate memory spaces.
This allows them to bypass the GIL
and achieve true parallelism for
CPU-bound tasks.
Each process has its own interpreter,
memory, and resources.
Communication between processes
can be done through inter-process
communication (IPC) mechanisms
like pipes, queues, and shared
memory.

@VEDANTSOLANKI
#PYTHONTIPS

Threads are suitable for scenarios


where tasks are I/O-bound, such as
downloading files, making HTTP
requests, or performing database
operations.
Since threads can release the GIL
during I/O operations, they can
achieve concurrency and improve
performance.
They are also beneficial for tasks that
involve a large number of relatively
small and independent operations.

@VEDANTSOLANKI
#PYTHONTIPS

Processes are ideal for CPU-bound


tasks that require intensive
computational work, such as data
processing, scientific calculations, or
simulations.
Since processes operate in separate
memory spaces and bypass the GIL,
they can achieve true parallelism and
utilize multiple CPU cores
effectively.
Processes are also useful when data
needs to be isolated, ensuring that
one process doesn't affect the
execution of another.

@VEDANTSOLANKI
#PYTHONTIPS

@VEDANTSOLANKI
#PYTHONTIPS

In above example, the main thread


starts the Fibonacci calculation in the
background using a separate thread.
While the Fibonacci sequence is
being calculated, the main thread
continues executing and prints the
message "Main thread continues
executing...".
Once the Fibonacci calculation is
complete, the sequence is printed,
and the main thread exits, printing
the message "Main thread exiting.".

@VEDANTSOLANKI
#PYTHONTIPS

@VEDANTSOLANKI
#PYTHONTIPS

In above example, the main process


creates multiple processes to
perform image processing on
different image files concurrently.
While the image processing is
happening in the background, the
main process continues executing
and prints the message "Main
process continues executing...".
Once all image processing is
completed, the processed image
paths are printed, and the main
process exits, printing the message
"Main process exiting.".

@VEDANTSOLANKI
#PYTHONTIPS

Threads represent sequences of


instructions within a process.
Threads share the same memory
space within a process.
Python threads are affected by the
Global Interpreter Lock (GIL),
limiting true parallelism for CPU-
bound tasks but allowing
concurrency for I/O-bound tasks.
Threads are lightweight and have
low overhead, making them suitable
for a large number of small tasks.
Threads can be used for I/O-bound
tasks and scenarios with a large
number of small and independent
operations.
@VEDANTSOLANKI
#PYTHONTIPS

Processes are independent instances


of programs running concurrently.
Processes have separate memory
spaces.
Python processes, managed by the
multiprocessing module, bypass the
GIL , have higher overhead due to
memory and resource isolation but
enable true parallelism.
Processes are recommended for
CPU-bound tasks and situations
where data isolation is required.

@VEDANTSOLANKI
@TAG
SOMEONE WHO

WILL FIND THIS

HELPFUL
FOLLOW FOR MORE !

Vedant Solanki
@vedantsolanki

You might also like