際際滷

際際滷Share a Scribd company logo
MultiThreading in Python
Multithreading
What are threads?
Thread is smallest unit that can be scheduled in an operating system.
Process vs Thread?
Multiple threads within a process share the same data space with the main thread
Threads sometimes called light-weight processes and they do not require much memory overhead
Create a Process:
Code: https://repl.it/IIpm/0
Threading in Python
There are two modules which support the usage of threads in Python:
Thread
Threading (We should use threading model instead of thread)
The module "thread" treats a thread as a function, while "threading module " is implemented in OOPS
way, i.e. every thread corresponds to an object.
Thread Module
we can use the function thread.start_new_thread:
thread.start_new_thread(function, args)
Method starts a new thread and return its identifier.Thread starts
and execute the handler function function with args as argument.
Args should be a tuple.
Code Example: https://repl.it/IIqg/3
Thread Lock
Discuss about atomic operation.What happens to global variable in case of
multithreading.
Problem in this code: https://repl.it/IIz1/0
Use lock_object = thread.allocate_lock()
And lock.acquire() and lock.release()
Code example: https://repl.it/IIyL/2
Threading Module
Threading module builds on the low-level features of thread to make working
with threads even easier and threads are implemented as objects.
Basic Example: https://repl.it/IJAB/3
Create a thread - Threading Module
Follow below steps:
Construct a subclass from the <Thread> class.
Override the <__init__(self [,args])> method to provide arguments.
override the <run(self [,args])> method to code the actual required logic of the thread.
When start is called on instance of Thread subclass,it eventually calls overridden run method.

More Related Content

What's hot (20)

Multithreading Concepts
Multithreading ConceptsMultithreading Concepts
Multithreading Concepts
Arvind Krishnaa
Java Multithreading Using Executors Framework
Java Multithreading Using Executors FrameworkJava Multithreading Using Executors Framework
Java Multithreading Using Executors Framework
Arun Mehra
Python multithreaded programming
Python   multithreaded programmingPython   multithreaded programming
Python multithreaded programming
Learnbay Datascience
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreading
jehan1987
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
Rajesh Ananda Kumar
Intro To .Net Threads
Intro To .Net ThreadsIntro To .Net Threads
Intro To .Net Threads
rchakra
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
Vikram Kalyani
Threading in C#
Threading in C#Threading in C#
Threading in C#
Medhat Dawoud
Java concurrency - Thread pools
Java concurrency - Thread poolsJava concurrency - Thread pools
Java concurrency - Thread pools
maksym220889
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
suraj pandey
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
junnubabu
Threads c sharp
Threads c sharpThreads c sharp
Threads c sharp
Deivaa
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
M. Raihan
Learning Java 3 油Threads and Synchronization
Learning Java 3 油Threads and SynchronizationLearning Java 3 油Threads and Synchronization
Learning Java 3 油Threads and Synchronization
caswenson
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Arafat Hossan
Threads c sharp
Threads c sharpThreads c sharp
Threads c sharp
Karthick Suresh
Concurrency in java
Concurrency in javaConcurrency in java
Concurrency in java
Abhra Basak
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
Rajkattamuri
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath
Multithreading Concepts
Multithreading ConceptsMultithreading Concepts
Multithreading Concepts
Arvind Krishnaa
Java Multithreading Using Executors Framework
Java Multithreading Using Executors FrameworkJava Multithreading Using Executors Framework
Java Multithreading Using Executors Framework
Arun Mehra
Python multithreaded programming
Python   multithreaded programmingPython   multithreaded programming
Python multithreaded programming
Learnbay Datascience
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreading
jehan1987
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
Rajesh Ananda Kumar
Intro To .Net Threads
Intro To .Net ThreadsIntro To .Net Threads
Intro To .Net Threads
rchakra
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
Vikram Kalyani
Java concurrency - Thread pools
Java concurrency - Thread poolsJava concurrency - Thread pools
Java concurrency - Thread pools
maksym220889
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
suraj pandey
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
junnubabu
Threads c sharp
Threads c sharpThreads c sharp
Threads c sharp
Deivaa
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
M. Raihan
Learning Java 3 油Threads and Synchronization
Learning Java 3 油Threads and SynchronizationLearning Java 3 油Threads and Synchronization
Learning Java 3 油Threads and Synchronization
caswenson
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Arafat Hossan
Concurrency in java
Concurrency in javaConcurrency in java
Concurrency in java
Abhra Basak
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
Rajkattamuri
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath

Similar to Python Training in Bangalore | Multi threading | Learnbay.in (20)

Multithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdfMultithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdf
giridharsripathi
Multithreading by rj
Multithreading by rjMultithreading by rj
Multithreading by rj
Shree M.L.Kakadiya MCA mahila college, Amreli
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
Rakesh Madugula
MULTI THREADING.pptx
MULTI THREADING.pptxMULTI THREADING.pptx
MULTI THREADING.pptx
KeerthanaM738437
Multi t hreading_14_10
Multi t hreading_14_10Multi t hreading_14_10
Multi t hreading_14_10
Minal Maniar
multithreading
multithreadingmultithreading
multithreading
Rajkattamuri
Multithreading
MultithreadingMultithreading
Multithreading
F K
Java
JavaJava
Java
mdfkhan625
Multithreading
MultithreadingMultithreading
Multithreading
Ravi Chythanya
Java
JavaJava
Java
Khasim Cise
Java multithreading
Java multithreadingJava multithreading
Java multithreading
Mohammed625
Engineeering Operating systemsOS UNIT 3 Threads.pptx
Engineeering Operating systemsOS UNIT 3 Threads.pptxEngineeering Operating systemsOS UNIT 3 Threads.pptx
Engineeering Operating systemsOS UNIT 3 Threads.pptx
ppkmurthy2006
Concurrency and parallel in .net
Concurrency and parallel in .netConcurrency and parallel in .net
Concurrency and parallel in .net
Mohammad Hossein Karami
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
Ganesh Samarthyam
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01
Aravindharamanan S
Understanding Threads in operating system
Understanding Threads in operating systemUnderstanding Threads in operating system
Understanding Threads in operating system
Harrytoye2
chap7_slidesforparallelcomputingananthgrama
chap7_slidesforparallelcomputingananthgramachap7_slidesforparallelcomputingananthgrama
chap7_slidesforparallelcomputingananthgrama
doomzday27
Threads and Synchronization in c#
Threads and Synchronization in c#Threads and Synchronization in c#
Threads and Synchronization in c#
Rizwan Ali
Sync, async and multithreading
Sync, async and multithreadingSync, async and multithreading
Sync, async and multithreading
Tuan Chau
Threads
ThreadsThreads
Threads
saanchi85
Multithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdfMultithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdf
giridharsripathi
Multi t hreading_14_10
Multi t hreading_14_10Multi t hreading_14_10
Multi t hreading_14_10
Minal Maniar
Multithreading
MultithreadingMultithreading
Multithreading
F K
Java multithreading
Java multithreadingJava multithreading
Java multithreading
Mohammed625
Engineeering Operating systemsOS UNIT 3 Threads.pptx
Engineeering Operating systemsOS UNIT 3 Threads.pptxEngineeering Operating systemsOS UNIT 3 Threads.pptx
Engineeering Operating systemsOS UNIT 3 Threads.pptx
ppkmurthy2006
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01
Aravindharamanan S
Understanding Threads in operating system
Understanding Threads in operating systemUnderstanding Threads in operating system
Understanding Threads in operating system
Harrytoye2
chap7_slidesforparallelcomputingananthgrama
chap7_slidesforparallelcomputingananthgramachap7_slidesforparallelcomputingananthgrama
chap7_slidesforparallelcomputingananthgrama
doomzday27
Threads and Synchronization in c#
Threads and Synchronization in c#Threads and Synchronization in c#
Threads and Synchronization in c#
Rizwan Ali
Sync, async and multithreading
Sync, async and multithreadingSync, async and multithreading
Sync, async and multithreading
Tuan Chau

Recently uploaded (20)

Purchase Analysis in Odoo 17 - Odoo 際際滷s
Purchase Analysis in Odoo 17 - Odoo 際際滷sPurchase Analysis in Odoo 17 - Odoo 際際滷s
Purchase Analysis in Odoo 17 - Odoo 際際滷s
Celine George
How to process Interwarehouse and Intrawarehouse transfers in Odoo
How to process Interwarehouse and Intrawarehouse transfers in OdooHow to process Interwarehouse and Intrawarehouse transfers in Odoo
How to process Interwarehouse and Intrawarehouse transfers in Odoo
Celine George
BIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptx
BIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptxBIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptx
BIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptx
maniramkumar
PATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINES
PATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINESPATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINES
PATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINES
BananaIP Counsels
Digital Electronics - Boolean Algebra (Module 2) - Dr. G.S. Virdi
Digital Electronics - Boolean Algebra (Module 2) - Dr. G.S. VirdiDigital Electronics - Boolean Algebra (Module 2) - Dr. G.S. Virdi
Digital Electronics - Boolean Algebra (Module 2) - Dr. G.S. Virdi
GS Virdi
2. ES: Application of Number: Units of measure & conversions
2. ES: Application of Number: Units of measure & conversions2. ES: Application of Number: Units of measure & conversions
2. ES: Application of Number: Units of measure & conversions
leigh441292
NC Advisory Council on Student Safety and Well-Being
NC Advisory Council on Student Safety and Well-BeingNC Advisory Council on Student Safety and Well-Being
NC Advisory Council on Student Safety and Well-Being
Mebane Rash
Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...
Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...
Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...
A Biodiction : A Unit of Dr. Divya Sharma
10.socialorganisationandsocialsystem .pptx
10.socialorganisationandsocialsystem .pptx10.socialorganisationandsocialsystem .pptx
10.socialorganisationandsocialsystem .pptx
Vivek Bhattji
MUSIC QUIZ | THE QUIZ CLUB OF PSGCAS | 12 MARCH 2025
MUSIC QUIZ | THE QUIZ CLUB OF PSGCAS | 12 MARCH 2025MUSIC QUIZ | THE QUIZ CLUB OF PSGCAS | 12 MARCH 2025
MUSIC QUIZ | THE QUIZ CLUB OF PSGCAS | 12 MARCH 2025
Quiz Club of PSG College of Arts & Science
How to configure the retail shop in Odoo 17 Point of Sale
How to configure the retail shop in Odoo 17 Point of SaleHow to configure the retail shop in Odoo 17 Point of Sale
How to configure the retail shop in Odoo 17 Point of Sale
Celine George
A-Z GENERAL QUIZ | THE QUIZ CLUB OF PSGCAS | 14TH MARCH 2025.pptx
A-Z GENERAL QUIZ | THE QUIZ CLUB OF PSGCAS | 14TH MARCH 2025.pptxA-Z GENERAL QUIZ | THE QUIZ CLUB OF PSGCAS | 14TH MARCH 2025.pptx
A-Z GENERAL QUIZ | THE QUIZ CLUB OF PSGCAS | 14TH MARCH 2025.pptx
Quiz Club of PSG College of Arts & Science
SUPPOSITORIES
SUPPOSITORIESSUPPOSITORIES
SUPPOSITORIES
Shantanu Ranjan
Anti-Protozoal Agents.pptx by Mrs. Manjushri P. Dabhade
Anti-Protozoal Agents.pptx by Mrs. Manjushri P. DabhadeAnti-Protozoal Agents.pptx by Mrs. Manjushri P. Dabhade
Anti-Protozoal Agents.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
Introduction to Drug Design.pptx by Mrs. Manjushri P. Dabhade
Introduction to Drug Design.pptx by Mrs. Manjushri P. DabhadeIntroduction to Drug Design.pptx by Mrs. Manjushri P. Dabhade
Introduction to Drug Design.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
technology in banking ppt FOR E-CONTENT -2.ppt
technology in banking ppt  FOR E-CONTENT -2.ppttechnology in banking ppt  FOR E-CONTENT -2.ppt
technology in banking ppt FOR E-CONTENT -2.ppt
HARIHARAN A
Mixed_Sinhala_Dual_Male_Names (1).pdf...
Mixed_Sinhala_Dual_Male_Names (1).pdf...Mixed_Sinhala_Dual_Male_Names (1).pdf...
Mixed_Sinhala_Dual_Male_Names (1).pdf...
keshanf79
UNIT 1 Introduction to communication.pptx
UNIT 1 Introduction to communication.pptxUNIT 1 Introduction to communication.pptx
UNIT 1 Introduction to communication.pptx
HARIHARAN A
Proteins, Bio similars & Antibodies.pptx
Proteins, Bio similars &  Antibodies.pptxProteins, Bio similars &  Antibodies.pptx
Proteins, Bio similars & Antibodies.pptx
Ashish Umale
Introduction to Karnaugh Maps (K-Maps) for Simplifying Boolean Expressions
Introduction to Karnaugh Maps (K-Maps) for Simplifying Boolean ExpressionsIntroduction to Karnaugh Maps (K-Maps) for Simplifying Boolean Expressions
Introduction to Karnaugh Maps (K-Maps) for Simplifying Boolean Expressions
GS Virdi
Purchase Analysis in Odoo 17 - Odoo 際際滷s
Purchase Analysis in Odoo 17 - Odoo 際際滷sPurchase Analysis in Odoo 17 - Odoo 際際滷s
Purchase Analysis in Odoo 17 - Odoo 際際滷s
Celine George
How to process Interwarehouse and Intrawarehouse transfers in Odoo
How to process Interwarehouse and Intrawarehouse transfers in OdooHow to process Interwarehouse and Intrawarehouse transfers in Odoo
How to process Interwarehouse and Intrawarehouse transfers in Odoo
Celine George
BIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptx
BIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptxBIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptx
BIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptx
maniramkumar
PATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINES
PATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINESPATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINES
PATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINES
BananaIP Counsels
Digital Electronics - Boolean Algebra (Module 2) - Dr. G.S. Virdi
Digital Electronics - Boolean Algebra (Module 2) - Dr. G.S. VirdiDigital Electronics - Boolean Algebra (Module 2) - Dr. G.S. Virdi
Digital Electronics - Boolean Algebra (Module 2) - Dr. G.S. Virdi
GS Virdi
2. ES: Application of Number: Units of measure & conversions
2. ES: Application of Number: Units of measure & conversions2. ES: Application of Number: Units of measure & conversions
2. ES: Application of Number: Units of measure & conversions
leigh441292
NC Advisory Council on Student Safety and Well-Being
NC Advisory Council on Student Safety and Well-BeingNC Advisory Council on Student Safety and Well-Being
NC Advisory Council on Student Safety and Well-Being
Mebane Rash
Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...
Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...
Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...
A Biodiction : A Unit of Dr. Divya Sharma
10.socialorganisationandsocialsystem .pptx
10.socialorganisationandsocialsystem .pptx10.socialorganisationandsocialsystem .pptx
10.socialorganisationandsocialsystem .pptx
Vivek Bhattji
How to configure the retail shop in Odoo 17 Point of Sale
How to configure the retail shop in Odoo 17 Point of SaleHow to configure the retail shop in Odoo 17 Point of Sale
How to configure the retail shop in Odoo 17 Point of Sale
Celine George
Anti-Protozoal Agents.pptx by Mrs. Manjushri P. Dabhade
Anti-Protozoal Agents.pptx by Mrs. Manjushri P. DabhadeAnti-Protozoal Agents.pptx by Mrs. Manjushri P. Dabhade
Anti-Protozoal Agents.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
Introduction to Drug Design.pptx by Mrs. Manjushri P. Dabhade
Introduction to Drug Design.pptx by Mrs. Manjushri P. DabhadeIntroduction to Drug Design.pptx by Mrs. Manjushri P. Dabhade
Introduction to Drug Design.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
technology in banking ppt FOR E-CONTENT -2.ppt
technology in banking ppt  FOR E-CONTENT -2.ppttechnology in banking ppt  FOR E-CONTENT -2.ppt
technology in banking ppt FOR E-CONTENT -2.ppt
HARIHARAN A
Mixed_Sinhala_Dual_Male_Names (1).pdf...
Mixed_Sinhala_Dual_Male_Names (1).pdf...Mixed_Sinhala_Dual_Male_Names (1).pdf...
Mixed_Sinhala_Dual_Male_Names (1).pdf...
keshanf79
UNIT 1 Introduction to communication.pptx
UNIT 1 Introduction to communication.pptxUNIT 1 Introduction to communication.pptx
UNIT 1 Introduction to communication.pptx
HARIHARAN A
Proteins, Bio similars & Antibodies.pptx
Proteins, Bio similars &  Antibodies.pptxProteins, Bio similars &  Antibodies.pptx
Proteins, Bio similars & Antibodies.pptx
Ashish Umale
Introduction to Karnaugh Maps (K-Maps) for Simplifying Boolean Expressions
Introduction to Karnaugh Maps (K-Maps) for Simplifying Boolean ExpressionsIntroduction to Karnaugh Maps (K-Maps) for Simplifying Boolean Expressions
Introduction to Karnaugh Maps (K-Maps) for Simplifying Boolean Expressions
GS Virdi

Python Training in Bangalore | Multi threading | Learnbay.in

  • 2. What are threads? Thread is smallest unit that can be scheduled in an operating system. Process vs Thread? Multiple threads within a process share the same data space with the main thread Threads sometimes called light-weight processes and they do not require much memory overhead Create a Process: Code: https://repl.it/IIpm/0
  • 3. Threading in Python There are two modules which support the usage of threads in Python: Thread Threading (We should use threading model instead of thread) The module "thread" treats a thread as a function, while "threading module " is implemented in OOPS way, i.e. every thread corresponds to an object.
  • 4. Thread Module we can use the function thread.start_new_thread: thread.start_new_thread(function, args) Method starts a new thread and return its identifier.Thread starts and execute the handler function function with args as argument. Args should be a tuple. Code Example: https://repl.it/IIqg/3
  • 5. Thread Lock Discuss about atomic operation.What happens to global variable in case of multithreading. Problem in this code: https://repl.it/IIz1/0 Use lock_object = thread.allocate_lock() And lock.acquire() and lock.release() Code example: https://repl.it/IIyL/2
  • 6. Threading Module Threading module builds on the low-level features of thread to make working with threads even easier and threads are implemented as objects. Basic Example: https://repl.it/IJAB/3
  • 7. Create a thread - Threading Module Follow below steps: Construct a subclass from the <Thread> class. Override the <__init__(self [,args])> method to provide arguments. override the <run(self [,args])> method to code the actual required logic of the thread. When start is called on instance of Thread subclass,it eventually calls overridden run method.