0% found this document useful (0 votes)
2 views8 pages

Multithreading in Python

The document provides an overview of multithreading in Python, explaining the concept of threads and how to create them using the threading module. It details methods for creating threads by inheriting the Thread class or using it directly, as well as synchronization techniques and the use of the sleep function. Additionally, it covers the join method and condition variables for thread communication.

Uploaded by

Komal Yadav
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Multithreading in Python

The document provides an overview of multithreading in Python, explaining the concept of threads and how to create them using the threading module. It details methods for creating threads by inheriting the Thread class or using it directly, as well as synchronization techniques and the use of the sleep function. Additionally, it covers the join method and condition variables for thread communication.

Uploaded by

Komal Yadav
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[Link]@yahoo.

com Naresh i Technologies

Multithreading
Multithreading:
Execution of more than one thread at a time is called as multithreading.
A thread is a piece of code that executes independently.
Every program contains atleast one thread i.e. main thread.
Main thread default name is MainThread only.

There are two ways to create a new thread:


1) By inheriting Thread class of threading module
2) Without inheriting Thread class of threading module

Steps to create a multithreaded application by inheriting Thread


class of threading module:
1) Import threading module
2) Create a class that inherits Thread class of threading module
3) Override run() method of Thread class
4) Write child thread task code in run() method
5) Create an object of current class
6) Call start() method of Thread class by using current object reference
Note: start() method implicitly calls run() method
7) Write main thread task code

Example:
import threading
class A([Link]):
def run(self):
for i in range(1,11):
print("Child Thread: ",i)

Python By Venkatesh Mansani Naresh i Technologies


[Link]@[Link] Naresh i Technologies

a=A()
[Link]()
for i in range(1,11):
print("Main Thread: ",i)

Methods of Thread class:


1) getName() method used to get the name of the thread
2) setName() method used to change the name of the thread

Function of threading module:


1) current_thread() function used to get the current thread object

Example:
import threading
class A([Link]):
def run(self):
for i in range(1,11):
print([Link](),": ",i)
a1=A()
[Link]("Child1")
[Link]()
a2=A()
[Link]("Child2")
[Link]()
t=threading.current_thread()
for i in range(1,11):
print([Link](),”:”,i)

Python By Venkatesh Mansani Naresh i Technologies


[Link]@[Link] Naresh i Technologies

Without inheriting Thread class of threading module:


Example:
from threading import *
def fun():
for i in range(10):
print("Child Thread Running.........")
t1=Thread(target=fun)
[Link]()
for i in range(10):
print("Main Thread Running...........")

join()method of Thread class:


This blocks the calling thread until the thread whose join() method is called is
terminated.

Example:
from threading import *
def fun():
for i in range(10):
print("Child Thread Running.........")
t1=Thread(target=fun)
[Link]()
[Link]()
print("End")

Python By Venkatesh Mansani Naresh i Technologies


[Link]@[Link] Naresh i Technologies

sleep() function of time module:


The sleep() function suspends execution of the current thread for a given number
of seconds.

Example1:
from threading import *
from time import *
def fun():
for i in range(10):
print(current_thread().getName(),i)
sleep(1)
t1=Thread(target=fun)
[Link]("Child")
[Link]()
current_thread().setName("Main")
for i in range(10):
print(current_thread().getName(),i)
sleep(1)

Example2:
import threading
import time
class A([Link]):
def run(self):
for i in range(1,11):
print("Child Thread: ",i)

Python By Venkatesh Mansani Naresh i Technologies


[Link]@[Link] Naresh i Technologies

[Link](1)
a=A()
[Link]()
for i in range(1,11):
print("Main Thread: ",i)
[Link](1)

Synchronization:
It is a mechanism that allows to access a shared resource only one thread at a
time.
By using acquire() & release() methods we can achieve synchronization.
acquire() method is used to acquire the lock. When it is invoked then it blocks
until the lock is unlocked.
release() method is used to release an acquired lock.
If the lock is locked, this method will reset it to unlocked and return.
This method can be called from any thread.
When this method is called, one out of the already waiting threads to acquire the
lock is allowed to hold the lock.

Example:
import threading
class A([Link]):
def run(self):
[Link]()
fun("Core")
[Link]()
class B([Link]):

Python By Venkatesh Mansani Naresh i Technologies


[Link]@[Link] Naresh i Technologies

def run(self):
[Link]()
fun("Advanced")
[Link]()
def fun(msg):
for i in range(10):
print("Welcome to")
print(msg)
print(" Python")
lc=[Link]()
a=A()
[Link]()
b=B()
[Link]()

wait() & notify() methods of Condition class of threading module:


wait() method is used to block the thread and wait until some other thread
notifies it by calling the notify() method.

Example:
import threading
class Bank:
def __init__(self):
[Link]=5000.00
def withdraw(self, amount):
[Link]()

Python By Venkatesh Mansani Naresh i Technologies


[Link]@[Link] Naresh i Technologies

print("Withdraw Started")
if [Link]<amount:
print("Less Balance, Waiting for Deposit")
[Link]()
[Link]=[Link]-amount
print("Withdraw Completed")
[Link]()
def deposit(self, amount):
[Link]()
print("Deposit Started")
[Link]=[Link]+amount
print("Deposit Completed")
[Link]()
[Link]()
class Customer1([Link]):
def __init__(self, b):
[Link].__init__(self)
self.b=b
def run(self):
[Link](8000.00)
class Customer2([Link]):
def __init__(self, b):
[Link].__init__(self)
self.b=b

Python By Venkatesh Mansani Naresh i Technologies


[Link]@[Link] Naresh i Technologies

def run(self):
[Link](5000.00)
c=[Link]()
b=Bank()
c1=Customer1(b)
[Link]()
c2=Customer2(b)
[Link]()
Note: wait() method must only be called when the calling thread has acquired the
lock.

By

Mr. Venkatesh Mansani


Naresh i Technologies

Python By Venkatesh Mansani Naresh i Technologies

You might also like