SlideShare a Scribd company logo
2
Most read
3
Most read
5
Most read
Swipe
Python - Multithreaded
Programming
Running several threads is similar to running
several different programs concurrently, but with
the following benefits
Multiple threads within a process share the
same data space with the main thread and can
therefore share information or communicate
with each other more easily than if they were
separate processes.
Threads sometimes called light-weight
processes and they do not require much
memory overhead; they are cheaper than
processes.
Python - Multithreaded Programming
A thread has a beginning, an execution sequence,
and a conclusion.
It has an instruction pointer that keeps track of
where within its context it is currently running.
It can be pre-empted (interrupted)
It can temporarily be put on hold (also known
as sleeping) while other threads are running -
this is called yielding.
To spawn another thread, you need to call
following method available in thread module
Starting a New Thread
thread.start_new_thread ( function, args[, kwargs] )
This method call enables a fast and efficient way
to create new threads in both Linux and Windows.
The method call returns immediately and the
child thread starts and calls function with the
passed list of args.
When function returns, the thread terminates.
The Threading Module


The newer threading module included with
Python 2.4 provides much more powerful, high-
level support for threads than the thread module
discussed in the previous section.
The threading module exposes all the methods of
the thread module and provides some additional
methods
threading.activeCount() − Returns the number
of thread objects that are active.
threading.currentThread() − Returns the
number of thread objects in the caller's thread
control.
threading.enumerate() − Returns a list of all
thread objects that are currently active.
In addition to the methods, the threading module
has the Thread class that implements threading.
The methods provided by the Thread class are as
follows
run() − The run() method is the entry point for
a thread.
start() − The start() method starts a thread by
calling the run method.
join([time]) − The join() waits for threads to
terminate.
isAlive() − The isAlive() method checks whether
a thread is still executing.
getName() − The getName() method returns
the name of a thread.
setName() − The setName() method sets the
name of a thread.
Creating Thread Using Threading Module
To implement a new thread using the threading
module, you have to do the following
Define a new subclass of the Thread class.
Override the __init__(self [,args]) method to add
additional arguments.
Then, override the run(self [,args]) method to
implement what the thread should do when
started.
Once you have created the new Thread subclass,
you can create an instance of it and then start a
new thread by invoking the start(), which in turn
calls run() method.
Synchronizing Threads
The threading module provided with Python
includes a simple-to-implement locking
mechanism that allows you to synchronize
threads.
A new lock is created by calling the Lock() method,
which returns the new lock.
The acquire(blocking) method of the new lock
object is used to force threads to run
synchronously.
The optional blocking parameter enables you to
control whether the thread waits to acquire the
lock.
If blocking is set to 0, the thread returns
immediately with a 0 value if the lock cannot be
acquired and with a 1 if the lock was acquired.
If blocking is set to 1, the thread blocks and wait
for the lock to be released.
Multithreaded Priority Queue


The Queue module allows you to create a new
queue object that can hold a specific number of
items. There are following methods to control the
Queue.
get() − The get() removes and returns an item
from the queue.
put() − The put adds item to a queue.
qsize() − The qsize() returns the number of
items that are currently in the queue.
empty() − The empty( ) returns True if queue is
empty; otherwise, False.
full() − the full() returns True if queue is full;
otherwise, False.
Python - GUI Programming
(Tkinter)
Statistic Probability
Stay Tuned with
Topics for next Post
Ad

Recommended

Queue
Queue
Budditha Hettige
 
File handling in Python
File handling in Python
Megha V
 
Python Generators
Python Generators
Akshar Raaj
 
File Handling Python
File Handling Python
Akhil Kaushik
 
7 distributed and real systems
7 distributed and real systems
myrajendra
 
Chapter 05 classes and objects
Chapter 05 classes and objects
Praveen M Jigajinni
 
Multiprocessor Systems
Multiprocessor Systems
vampugani
 
Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
Introduction to Programming in LISP
Introduction to Programming in LISP
Knoldus Inc.
 
Object oriented programming concepts
Object oriented programming concepts
rahuld115
 
Operating system critical section
Operating system critical section
Harshana Madusanka Jayamaha
 
DBMS (Deadlock, deadlock prevention, 2phase locking)
DBMS (Deadlock, deadlock prevention, 2phase locking)
Gaurav Solanki
 
Core java complete ppt(note)
Core java complete ppt(note)
arvind pandey
 
Open mp
Open mp
Gopi Saiteja
 
File access methods.54
File access methods.54
myrajendra
 
Distributed system architecture
Distributed system architecture
Yisal Khan
 
Arrays in Java
Arrays in Java
Naz Abdalla
 
Classes and Objects
Classes and Objects
yndaravind
 
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
Edureka!
 
Python file handling
Python file handling
Prof. Dr. K. Adisesha
 
Memory Management in OS
Memory Management in OS
Kumar Pritam
 
Identifying classes and objects ooad
Identifying classes and objects ooad
Melba Rosalind
 
Transaction management DBMS
Transaction management DBMS
Megha Patel
 
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION
sathish sak
 
Contiguous Memory Allocation.ppt
Contiguous Memory Allocation.ppt
infomerlin
 
Introduction to Object Oriented Programming
Introduction to Object Oriented Programming
Moutaz Haddara
 
Queue in Data Structure
Queue in Data Structure
Muhazzab Chouhadry
 
The Object Model
The Object Model
yndaravind
 
Python multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugam
Navaneethan Naveen
 
Python multithreading
Python multithreading
Smt. Indira Gandhi College of Engineering, Navi Mumbai, Mumbai
 

More Related Content

What's hot (20)

Introduction to Programming in LISP
Introduction to Programming in LISP
Knoldus Inc.
 
Object oriented programming concepts
Object oriented programming concepts
rahuld115
 
Operating system critical section
Operating system critical section
Harshana Madusanka Jayamaha
 
DBMS (Deadlock, deadlock prevention, 2phase locking)
DBMS (Deadlock, deadlock prevention, 2phase locking)
Gaurav Solanki
 
Core java complete ppt(note)
Core java complete ppt(note)
arvind pandey
 
Open mp
Open mp
Gopi Saiteja
 
File access methods.54
File access methods.54
myrajendra
 
Distributed system architecture
Distributed system architecture
Yisal Khan
 
Arrays in Java
Arrays in Java
Naz Abdalla
 
Classes and Objects
Classes and Objects
yndaravind
 
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
Edureka!
 
Python file handling
Python file handling
Prof. Dr. K. Adisesha
 
Memory Management in OS
Memory Management in OS
Kumar Pritam
 
Identifying classes and objects ooad
Identifying classes and objects ooad
Melba Rosalind
 
Transaction management DBMS
Transaction management DBMS
Megha Patel
 
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION
sathish sak
 
Contiguous Memory Allocation.ppt
Contiguous Memory Allocation.ppt
infomerlin
 
Introduction to Object Oriented Programming
Introduction to Object Oriented Programming
Moutaz Haddara
 
Queue in Data Structure
Queue in Data Structure
Muhazzab Chouhadry
 
The Object Model
The Object Model
yndaravind
 
Introduction to Programming in LISP
Introduction to Programming in LISP
Knoldus Inc.
 
Object oriented programming concepts
Object oriented programming concepts
rahuld115
 
DBMS (Deadlock, deadlock prevention, 2phase locking)
DBMS (Deadlock, deadlock prevention, 2phase locking)
Gaurav Solanki
 
Core java complete ppt(note)
Core java complete ppt(note)
arvind pandey
 
File access methods.54
File access methods.54
myrajendra
 
Distributed system architecture
Distributed system architecture
Yisal Khan
 
Classes and Objects
Classes and Objects
yndaravind
 
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
Edureka!
 
Memory Management in OS
Memory Management in OS
Kumar Pritam
 
Identifying classes and objects ooad
Identifying classes and objects ooad
Melba Rosalind
 
Transaction management DBMS
Transaction management DBMS
Megha Patel
 
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION
sathish sak
 
Contiguous Memory Allocation.ppt
Contiguous Memory Allocation.ppt
infomerlin
 
Introduction to Object Oriented Programming
Introduction to Object Oriented Programming
Moutaz Haddara
 
The Object Model
The Object Model
yndaravind
 

Similar to Python multithreaded programming (20)

Python multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugam
Navaneethan Naveen
 
Python multithreading
Python multithreading
Smt. Indira Gandhi College of Engineering, Navi Mumbai, Mumbai
 
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
Mohammad Reza Kamalifard
 
Thread model in java
Thread model in java
AmbigaMurugesan
 
MULTI THREADING.pptx
MULTI THREADING.pptx
KeerthanaM738437
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
nimbalkarvikram966
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
Threading concepts
Threading concepts
Raheemaparveen
 
Multithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming language
arnavytstudio2814
 
Md09 multithreading
Md09 multithreading
Rakesh Madugula
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
Arulmozhivarman8
 
Multithreading Presentation
Multithreading Presentation
Neeraj Kaushik
 
Concept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptx
SahilKumar542
 
Threadnotes
Threadnotes
Himanshu Rajput
 
Module - 5 merged.docx notes about engineering subjects java
Module - 5 merged.docx notes about engineering subjects java
KaviShetty
 
Java concurrency - Thread pools
Java concurrency - Thread pools
maksym220889
 
Inter thread communication & runnable interface
Inter thread communication & runnable interface
keval_thummar
 
Java And Multithreading
Java And Multithreading
Shraddha
 
Multi threading
Multi threading
Mavoori Soshmitha
 
Multithreading in java
Multithreading in java
Kavitha713564
 
Python multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugam
Navaneethan Naveen
 
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
Mohammad Reza Kamalifard
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
nimbalkarvikram966
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
Multithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming language
arnavytstudio2814
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
Arulmozhivarman8
 
Multithreading Presentation
Multithreading Presentation
Neeraj Kaushik
 
Concept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptx
SahilKumar542
 
Module - 5 merged.docx notes about engineering subjects java
Module - 5 merged.docx notes about engineering subjects java
KaviShetty
 
Java concurrency - Thread pools
Java concurrency - Thread pools
maksym220889
 
Inter thread communication & runnable interface
Inter thread communication & runnable interface
keval_thummar
 
Java And Multithreading
Java And Multithreading
Shraddha
 
Multithreading in java
Multithreading in java
Kavitha713564
 
Ad

More from Learnbay Datascience (20)

Top data science projects
Top data science projects
Learnbay Datascience
 
Python my SQL - create table
Python my SQL - create table
Learnbay Datascience
 
Python my SQL - create database
Python my SQL - create database
Learnbay Datascience
 
Python my sql database connection
Python my sql database connection
Learnbay Datascience
 
Python - mySOL
Python - mySOL
Learnbay Datascience
 
AI - Issues and Terminology
AI - Issues and Terminology
Learnbay Datascience
 
AI - Fuzzy Logic Systems
AI - Fuzzy Logic Systems
Learnbay Datascience
 
AI - working of an ns
AI - working of an ns
Learnbay Datascience
 
Artificial Intelligence- Neural Networks
Artificial Intelligence- Neural Networks
Learnbay Datascience
 
AI - Robotics
AI - Robotics
Learnbay Datascience
 
Applications of expert system
Applications of expert system
Learnbay Datascience
 
Components of expert systems
Components of expert systems
Learnbay Datascience
 
Artificial intelligence - expert systems
Artificial intelligence - expert systems
Learnbay Datascience
 
AI - natural language processing
AI - natural language processing
Learnbay Datascience
 
Ai popular search algorithms
Ai popular search algorithms
Learnbay Datascience
 
AI - Agents & Environments
AI - Agents & Environments
Learnbay Datascience
 
Artificial intelligence - research areas
Artificial intelligence - research areas
Learnbay Datascience
 
Artificial intelligence composed
Artificial intelligence composed
Learnbay Datascience
 
Artificial intelligence intelligent systems
Artificial intelligence intelligent systems
Learnbay Datascience
 
Applications of ai
Applications of ai
Learnbay Datascience
 
Artificial Intelligence- Neural Networks
Artificial Intelligence- Neural Networks
Learnbay Datascience
 
Artificial intelligence - expert systems
Artificial intelligence - expert systems
Learnbay Datascience
 
Artificial intelligence - research areas
Artificial intelligence - research areas
Learnbay Datascience
 
Artificial intelligence intelligent systems
Artificial intelligence intelligent systems
Learnbay Datascience
 
Ad

Recently uploaded (20)

Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
RAKESH SAJJAN
 
Wax Moon, Richmond, VA. Terrence McPherson
Wax Moon, Richmond, VA. Terrence McPherson
TerrenceMcPherson1
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
RAKESH SAJJAN
 
Wax Moon, Richmond, VA. Terrence McPherson
Wax Moon, Richmond, VA. Terrence McPherson
TerrenceMcPherson1
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 

Python multithreaded programming

  • 2. Running several threads is similar to running several different programs concurrently, but with the following benefits Multiple threads within a process share the same data space with the main thread and can therefore share information or communicate with each other more easily than if they were separate processes. Threads sometimes called light-weight processes and they do not require much memory overhead; they are cheaper than processes. Python - Multithreaded Programming
  • 3. A thread has a beginning, an execution sequence, and a conclusion. It has an instruction pointer that keeps track of where within its context it is currently running. It can be pre-empted (interrupted) It can temporarily be put on hold (also known as sleeping) while other threads are running - this is called yielding.
  • 4. To spawn another thread, you need to call following method available in thread module Starting a New Thread thread.start_new_thread ( function, args[, kwargs] ) This method call enables a fast and efficient way to create new threads in both Linux and Windows. The method call returns immediately and the child thread starts and calls function with the passed list of args. When function returns, the thread terminates.
  • 5. The Threading Module The newer threading module included with Python 2.4 provides much more powerful, high- level support for threads than the thread module discussed in the previous section. The threading module exposes all the methods of the thread module and provides some additional methods threading.activeCount() − Returns the number of thread objects that are active. threading.currentThread() − Returns the number of thread objects in the caller's thread control. threading.enumerate() − Returns a list of all thread objects that are currently active.
  • 6. In addition to the methods, the threading module has the Thread class that implements threading. The methods provided by the Thread class are as follows run() − The run() method is the entry point for a thread. start() − The start() method starts a thread by calling the run method. join([time]) − The join() waits for threads to terminate. isAlive() − The isAlive() method checks whether a thread is still executing. getName() − The getName() method returns the name of a thread. setName() − The setName() method sets the name of a thread.
  • 7. Creating Thread Using Threading Module To implement a new thread using the threading module, you have to do the following Define a new subclass of the Thread class. Override the __init__(self [,args]) method to add additional arguments. Then, override the run(self [,args]) method to implement what the thread should do when started. Once you have created the new Thread subclass, you can create an instance of it and then start a new thread by invoking the start(), which in turn calls run() method.
  • 8. Synchronizing Threads The threading module provided with Python includes a simple-to-implement locking mechanism that allows you to synchronize threads. A new lock is created by calling the Lock() method, which returns the new lock. The acquire(blocking) method of the new lock object is used to force threads to run synchronously. The optional blocking parameter enables you to control whether the thread waits to acquire the lock.
  • 9. If blocking is set to 0, the thread returns immediately with a 0 value if the lock cannot be acquired and with a 1 if the lock was acquired. If blocking is set to 1, the thread blocks and wait for the lock to be released.
  • 10. Multithreaded Priority Queue The Queue module allows you to create a new queue object that can hold a specific number of items. There are following methods to control the Queue. get() − The get() removes and returns an item from the queue. put() − The put adds item to a queue. qsize() − The qsize() returns the number of items that are currently in the queue. empty() − The empty( ) returns True if queue is empty; otherwise, False. full() − the full() returns True if queue is full; otherwise, False.
  • 11. Python - GUI Programming (Tkinter) Statistic Probability Stay Tuned with Topics for next Post