SlideShare a Scribd company logo
2
Most read
9
Most read
11
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

More Related Content

PPTX
Java Stack Data Structure.pptx
PPS
Wrapper class
PPT
Thread model in java
PPTX
String Builder & String Buffer (Java Programming)
PPTX
Java constructors
PPTX
Regular expressions in Python
PPTX
Chapter 05 classes and objects
PDF
Strings in python
Java Stack Data Structure.pptx
Wrapper class
Thread model in java
String Builder & String Buffer (Java Programming)
Java constructors
Regular expressions in Python
Chapter 05 classes and objects
Strings in python

What's hot (20)

ODP
Java Collections
PPTX
Class, object and inheritance in python
PPTX
Python: Modules and Packages
PPTX
Method overloading
PDF
Strings in java
PPT
MySQL Functions
PPTX
Introduction to numpy Session 1
PDF
Python list
PDF
C++ OOPS Concept
PPTX
The data mining query language
PDF
Network programming Using Python
PDF
What is Python Lambda Function? Python Tutorial | Edureka
PPTX
Circular linked list
PDF
Arrays in Java
PDF
Python-02| Input, Output & Import
PPT
Method overriding
PDF
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
PPT
Java collections concept
Java Collections
Class, object and inheritance in python
Python: Modules and Packages
Method overloading
Strings in java
MySQL Functions
Introduction to numpy Session 1
Python list
C++ OOPS Concept
The data mining query language
Network programming Using Python
What is Python Lambda Function? Python Tutorial | Edureka
Circular linked list
Arrays in Java
Python-02| Input, Output & Import
Method overriding
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
Java collections concept
Ad

Similar to Python multithreaded programming (20)

PPT
Python multithreading session 9 - shanmugam
PPTX
MULTI THREADING.pptx
PPTX
Python UNIT-IV Multi Threading B.Tech CSE
PPTX
Generators & Decorators.pptx
PDF
Multithreaded_Programming_in_Python.pdf
PDF
concurrency
PPTX
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
PDF
MultiThreading in Python
PDF
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
PPTX
Mathemetics module
PPTX
MULTI-THREADING in python appalication.pptx
PPTX
multithreadingppt.pptx
PDF
Python programming : Threads
DOCX
Multi Threading.docx
PDF
Multiprocessing.pdf..............,.......
PDF
Multiprocessing.............,...........
PDF
Multiprocessing with python
PDF
Concurrency and Python - PyCon MY 2015
Python multithreading session 9 - shanmugam
MULTI THREADING.pptx
Python UNIT-IV Multi Threading B.Tech CSE
Generators & Decorators.pptx
Multithreaded_Programming_in_Python.pdf
concurrency
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
MultiThreading in Python
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
Mathemetics module
MULTI-THREADING in python appalication.pptx
multithreadingppt.pptx
Python programming : Threads
Multi Threading.docx
Multiprocessing.pdf..............,.......
Multiprocessing.............,...........
Multiprocessing with python
Concurrency and Python - PyCon MY 2015
Ad

More from Learnbay Datascience (20)

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

Recently uploaded (20)

PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Classroom Observation Tools for Teachers
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Complications of Minimal Access Surgery at WLH
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Renaissance Architecture: A Journey from Faith to Humanism
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
human mycosis Human fungal infections are called human mycosis..pptx
O7-L3 Supply Chain Operations - ICLT Program
Supply Chain Operations Speaking Notes -ICLT Program
Microbial diseases, their pathogenesis and prophylaxis
FourierSeries-QuestionsWithAnswers(Part-A).pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Classroom Observation Tools for Teachers
VCE English Exam - Section C Student Revision Booklet
Complications of Minimal Access Surgery at WLH
01-Introduction-to-Information-Management.pdf
Cell Structure & Organelles in detailed.
102 student loan defaulters named and shamed – Is someone you know on the list?
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx

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