SlideShare a Scribd company logo
2
Most read
3
Most read
MULTI-THREADING in python appalication.pptx
MULTITHREADING
• Multithreading is defined as the ability of a processor to
execute multiple threads concurrently.
• In a simple, single-core CPU, it is achieved using frequent
switching between threads. This is termed context
switching.
• In context switching, the state of a thread is saved and the
state of another thread is loaded whenever any interrupt
(due to I/O or manually set) takes place.
• Context switching takes place so frequently that all the
threads appear to be running parallelly (this is
termed multitasking).
PYTHON THREADING
• A thread is an entity within a process that can be scheduled for execution.
• Also, it is the smallest unit of processing that can be performed in an OS (Operating System).
• In simple words, a thread is a sequence of such instructions within a program that can be executed independently of
other code.
• For simplicity, you can assume that a thread is simply a subset of a process!
A thread contains all this information in a Thread Control Block (TCB):
•Thread Identifier: Unique id (TID) is assigned to every new thread
•Stack pointer: Points to the thread’s stack in the process. The stack contains the local variables under the thread’s
scope.
•Program counter: a register that stores the address of the instruction currently being executed by a thread.
•Thread state: can be running, ready, waiting, starting, or done.
•Thread’s register set: registers assigned to thread for computations.
•Parent process Pointer: A pointer to the Process control block (PCB) of the process that the thread lives on.
PROCESS AND ITS THREAD
Multiple threads can exist within one process where:
•Each thread contains its own register set and local variables
(stored in the stack).
•All threads of a process share global variables (stored in
heap) and the program code.
MULTITHREADING IN PYTHON
Step 1: Import Module
First, import the threading module.
import threading
Step 2: Create a Thread
To create a new thread, we create an object of the Thread class. It takes the ‘target’ and ‘args’ as the parameters. The target is the function to be
executed by the thread whereas the args is the arguments to be passed to the target function.
t1 = threading.Thread(target, args)
t2 = threading.Thread(target, args)
Step 3: Start a Thread
To start a thread, we use the start() method of the Thread class.
t1.start()
t2.start()
Step 4: End the thread Execution
Once the threads start, the current program (you can think of it like a main thread) also keeps on executing. In order to stop the execution of the
current program until a thread is complete, we use the join() method.
t1.join()
t2.join()
As a result, the current program will first wait for the completion of t1 and then t2. Once, they are finished, the remaining statements of the
current program are executed.
• os.getpid() - to get the ID of the current process.
• threading.main_thread() - to get the main thread object.
• In normal conditions, the main thread is the thread from which the Python interpreter was started.
• name attribute of the thread object is used to get the name of the thread.
• threading.current_thread() - to get the current thread object.
The threads run in parallel, and the code provides information about the process ID and thread names.
PROGRAM
Creating a server and pinging the server with a message using multithreading
CODE WALKTHROUGH
• Importing necessary modules: The program starts by
importing the necessary Python modules - `threading` and
`socket`.
• Define the ClientThread class: This class inherits from the
`threading.Thread` class and represents a single client
connection. Each instance of this class will handle one
client.
• Create a socket: A socket is created using the
`socket.socket()` function. This socket will be used to
listen for incoming connections.
• Bind the socket: The `bind()` function is used to bind the
socket to a specific network interface and port number.
• Listen for connections: The server starts listening for
incoming connections with the `listen()` function.
• Accept connections and create new threads: The server
enters a loop where it waits for a new connection, accepts
it with the `accept()` function, and then creates a new
`ClientThread` to handle the connection.
CODE WALKTHROUGH
• Importing socket module in this file as well.
• Creating a socket named ‘client_socket’ and creating using
‘socket.AF_INET, socket.SOCK_STREAM’
• the line `client_socket = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)` creates a new TCP/IP socket
object that can be used for communication over the
Internet using IPv4 addresses and the TCP protocol.
• In the context of the `socket` module in Python,
`socket.AF_INET` and `socket.SOCK_STREAM` are
constants used to specify the address family and socket
type when creating a new socket object.
• Then connecting this file to the server we have created
before.
• Sending message "Hey I'm here ;)" from this file to the
server.
• After the message is sent closing the server using
‘client_socket.close()’.
RUNTIME VIDEO
CONCLUSION
• We all know how important web data is these days. But fetching data from websites can be a real time sink,
especially when you need to grab information from multiple sources.
• That's where multithreading comes in handy. By dividing the work among multiple threads, we can speed up the
process and fetch data concurrently from different websites.
• Web scraping involves fetching data from websites programmatically.
• Fetching data from multiple websites can be time-consuming when done sequentially.
• Multithreading can improve performance by fetching data and the data is fetched concurrently while improving
the performance.
THANK YOU

More Related Content

PPTX
Coding standard and coding guideline
PDF
Java 8 features
PDF
Collections In Java
PPTX
Arrays,string and vector
PPTX
Arrays in java
PPTX
Java 8 - Features Overview
PPTX
Angularjs PPT
Coding standard and coding guideline
Java 8 features
Collections In Java
Arrays,string and vector
Arrays in java
Java 8 - Features Overview
Angularjs PPT

What's hot (20)

PDF
React Server Side Rendering with Next.js
PPTX
Web forms in ASP.net
PPTX
C# Dictionary Hash Table and sets
PPTX
Super keyword in java
PDF
Asp.net state management
PPTX
Typescript ppt
PDF
Wrapper classes
PPTX
Exception handling in JAVA
PDF
Class and Objects in Java
PPTX
Exception handling
PPTX
Coding standards
PPT
Operator Overloading
PPS
Java Exception handling
PPTX
Vectors in Java
PPTX
Angular interview questions
PPTX
Android Services
PPT
Types of exceptions
PPTX
Angular modules in depth
PPTX
Test Classes in Salesforce
PPTX
Calender in asp.net
React Server Side Rendering with Next.js
Web forms in ASP.net
C# Dictionary Hash Table and sets
Super keyword in java
Asp.net state management
Typescript ppt
Wrapper classes
Exception handling in JAVA
Class and Objects in Java
Exception handling
Coding standards
Operator Overloading
Java Exception handling
Vectors in Java
Angular interview questions
Android Services
Types of exceptions
Angular modules in depth
Test Classes in Salesforce
Calender in asp.net
Ad

Similar to MULTI-THREADING in python appalication.pptx (20)

PPTX
MULTI THREADING.pptx
PDF
Multithreaded_Programming_in_Python.pdf
PPTX
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
PPTX
Python UNIT-IV Multi Threading B.Tech CSE
PDF
Concurrency in Python
PDF
MultiThreading in Python
PPTX
Generators & Decorators.pptx
PDF
concurrency
PDF
Concurrency and Python - PyCon MY 2015
DOCX
Multi Threading.docx
PDF
Python multithreaded programming
PDF
Multiprocessing.pdf..............,.......
PDF
Multiprocessing.............,...........
PPT
Python multithreading session 9 - shanmugam
PDF
Python programming : Threads
PDF
Multiprocessing with python
PDF
Pthread
MULTI THREADING.pptx
Multithreaded_Programming_in_Python.pdf
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Python UNIT-IV Multi Threading B.Tech CSE
Concurrency in Python
MultiThreading in Python
Generators & Decorators.pptx
concurrency
Concurrency and Python - PyCon MY 2015
Multi Threading.docx
Python multithreaded programming
Multiprocessing.pdf..............,.......
Multiprocessing.............,...........
Python multithreading session 9 - shanmugam
Python programming : Threads
Multiprocessing with python
Pthread
Ad

Recently uploaded (20)

PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Pre independence Education in Inndia.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Structure & Organelles in detailed.
PDF
Business Ethics Teaching Materials for college
PDF
RMMM.pdf make it easy to upload and study
PPTX
master seminar digital applications in india
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Basic Mud Logging Guide for educational purpose
Anesthesia in Laparoscopic Surgery in India
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Pre independence Education in Inndia.pdf
01-Introduction-to-Information-Management.pdf
Week 4 Term 3 Study Techniques revisited.pptx
Microbial disease of the cardiovascular and lymphatic systems
Cell Structure & Organelles in detailed.
Business Ethics Teaching Materials for college
RMMM.pdf make it easy to upload and study
master seminar digital applications in india
Renaissance Architecture: A Journey from Faith to Humanism
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
STATICS OF THE RIGID BODIES Hibbelers.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPH.pptx obstetrics and gynecology in nursing
Final Presentation General Medicine 03-08-2024.pptx
O7-L3 Supply Chain Operations - ICLT Program
Basic Mud Logging Guide for educational purpose

MULTI-THREADING in python appalication.pptx

  • 2. MULTITHREADING • Multithreading is defined as the ability of a processor to execute multiple threads concurrently. • In a simple, single-core CPU, it is achieved using frequent switching between threads. This is termed context switching. • In context switching, the state of a thread is saved and the state of another thread is loaded whenever any interrupt (due to I/O or manually set) takes place. • Context switching takes place so frequently that all the threads appear to be running parallelly (this is termed multitasking).
  • 3. PYTHON THREADING • A thread is an entity within a process that can be scheduled for execution. • Also, it is the smallest unit of processing that can be performed in an OS (Operating System). • In simple words, a thread is a sequence of such instructions within a program that can be executed independently of other code. • For simplicity, you can assume that a thread is simply a subset of a process! A thread contains all this information in a Thread Control Block (TCB): •Thread Identifier: Unique id (TID) is assigned to every new thread •Stack pointer: Points to the thread’s stack in the process. The stack contains the local variables under the thread’s scope. •Program counter: a register that stores the address of the instruction currently being executed by a thread. •Thread state: can be running, ready, waiting, starting, or done. •Thread’s register set: registers assigned to thread for computations. •Parent process Pointer: A pointer to the Process control block (PCB) of the process that the thread lives on.
  • 4. PROCESS AND ITS THREAD Multiple threads can exist within one process where: •Each thread contains its own register set and local variables (stored in the stack). •All threads of a process share global variables (stored in heap) and the program code.
  • 5. MULTITHREADING IN PYTHON Step 1: Import Module First, import the threading module. import threading Step 2: Create a Thread To create a new thread, we create an object of the Thread class. It takes the ‘target’ and ‘args’ as the parameters. The target is the function to be executed by the thread whereas the args is the arguments to be passed to the target function. t1 = threading.Thread(target, args) t2 = threading.Thread(target, args) Step 3: Start a Thread To start a thread, we use the start() method of the Thread class. t1.start() t2.start() Step 4: End the thread Execution Once the threads start, the current program (you can think of it like a main thread) also keeps on executing. In order to stop the execution of the current program until a thread is complete, we use the join() method. t1.join() t2.join() As a result, the current program will first wait for the completion of t1 and then t2. Once, they are finished, the remaining statements of the current program are executed.
  • 6. • os.getpid() - to get the ID of the current process. • threading.main_thread() - to get the main thread object. • In normal conditions, the main thread is the thread from which the Python interpreter was started. • name attribute of the thread object is used to get the name of the thread. • threading.current_thread() - to get the current thread object. The threads run in parallel, and the code provides information about the process ID and thread names. PROGRAM
  • 7. Creating a server and pinging the server with a message using multithreading
  • 8. CODE WALKTHROUGH • Importing necessary modules: The program starts by importing the necessary Python modules - `threading` and `socket`. • Define the ClientThread class: This class inherits from the `threading.Thread` class and represents a single client connection. Each instance of this class will handle one client. • Create a socket: A socket is created using the `socket.socket()` function. This socket will be used to listen for incoming connections. • Bind the socket: The `bind()` function is used to bind the socket to a specific network interface and port number. • Listen for connections: The server starts listening for incoming connections with the `listen()` function. • Accept connections and create new threads: The server enters a loop where it waits for a new connection, accepts it with the `accept()` function, and then creates a new `ClientThread` to handle the connection.
  • 9. CODE WALKTHROUGH • Importing socket module in this file as well. • Creating a socket named ‘client_socket’ and creating using ‘socket.AF_INET, socket.SOCK_STREAM’ • the line `client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)` creates a new TCP/IP socket object that can be used for communication over the Internet using IPv4 addresses and the TCP protocol. • In the context of the `socket` module in Python, `socket.AF_INET` and `socket.SOCK_STREAM` are constants used to specify the address family and socket type when creating a new socket object. • Then connecting this file to the server we have created before. • Sending message "Hey I'm here ;)" from this file to the server. • After the message is sent closing the server using ‘client_socket.close()’.
  • 11. CONCLUSION • We all know how important web data is these days. But fetching data from websites can be a real time sink, especially when you need to grab information from multiple sources. • That's where multithreading comes in handy. By dividing the work among multiple threads, we can speed up the process and fetch data concurrently from different websites. • Web scraping involves fetching data from websites programmatically. • Fetching data from multiple websites can be time-consuming when done sequentially. • Multithreading can improve performance by fetching data and the data is fetched concurrently while improving the performance.