What is aProcess?
What is a Thread?
• A thread is a sequence of instructions that the computer
performs.
• A program in execution is known as a process.
• When you start any app or program on your computer, such as the internet browser, the
operating system treats it as a process.
• A process may consist of several threads of execution that may execute concurrently.
What is Multithreading in
Python?
• 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.
Starting a NewThread
• Python offers a standard library called "threading" to perform
multithreading in Python.
• In Python multithreading, there are two ways in which you can
start a new thread-
1. Using the Threading Module
2. Using the Thread Module
6.
Race Conditions
• Whentwo or more threads access the shared piece of data
and resource.
• The solution to this is synchronizing threads
A Lock ensures that only one thread
can access a particular block of code
at a time
lock.acquire(): The thread that runs this
line will acquire the lock, meaning no
other thread can enter the critical section
(the code that modifies the shared
resource) until the lock is released.
lock.release(): This releases the lock so that the next
waiting thread can acquire it and proceed.
7.
Global Interpreter Lock(GIL)
• The Global Interpreter Lock (GIL) is a mechanism in Python that
allows only one thread to execute at a time in the interpreter.
Consider a shared variable n: n = 5
Suppose two threads, T1 and T2, modify n as follows:
# T1
n = n + 20
# T2
n = n * 10
The final value of n depends on the execution order of T1 and T2. For instance,
if T1 executes before T2, the result differs from if T2 goes first. In the worst case, if both
threads access n simultaneously, both might read n as 5, leading to inconsistent
modifications.
8.
The Impact onMulti-Threaded
Python Programs- GIL
• GIL prevents this by ensuring that only one thread runs at a
time, making Python's thread management simpler and safer.
However, this comes at the cost of reduced efficiency in multi-
core systems, as threads cannot run in parallel, limiting the
utilization of multiple processors.
•CPU Bound: These programs, such as image processing or
complex mathematical computations, push the CPU to its limits.
They do not heavily rely on I/O operations.
•I/O Bound: These involve significant I/O operations, like file or
network access, often waiting for external input/output.
9.
How to Dealwith Python's GIL?
1.Using Multiprocessing:
By creating multiple processes instead of threads, each process
operates with its own Python interpreter and memory space,
thereby sidestepping the GIL. However, this approach should be
used judiciously as processes are more resource-intensive than
threads, and managing multiple processes can introduce significant
overhead.
10.
How to Dealwith Python's GIL?
Choosing a Different Interpreter: Python supports various
interpreters, each with its own characteristics:
1.CPython: The standard interpreter, written in C, includes GIL.
2.JPython: A Java-based interpreter.
3.IronPython: Developed in C#, primarily for .NET environments.
4.PyPy: An alternative Python interpreter, implemented in Python,
focusing on performance and efficiency.
1.Except for CPython, other interpreters may not have a GIL,
offering potential performance benefits. However, compatibility
with existing libraries is a critical factor to consider when
switching interpreters, as not all Python libraries may be
available or function identically across different interpreters.
11.
Threading Objects
1. Semaphore:
•A semaphore is a synchronization tool that controls access to a shared resource by multiple threads.
• It maintains a counter to track how many threads can access the resource concurrently.
• Once the count reaches zero, any further threads attempting to access the resource are blocked until
another thread releases the semaphore.
2. Timer:
• A timer is a thread that is set to execute a function after a specified amount of time.
• It’s useful when you need to delay the execution of a function for a certain period
3. Barrier:
• A barrier is a synchronization tool that pauses multiple threads at a certain point until all threads
reach the barrier.
• Once the specified number of threads reaches the barrier, they all proceed together.