0% found this document useful (0 votes)
2 views

9 Exp Python

The document outlines an academic experiment focused on writing Python programs utilizing threading, aimed at enhancing students' understanding of multithreading concepts. It discusses the benefits of multithreading, when to use it, and provides practical examples of implementing threading in Python using the threading module. Additionally, it covers essential methods like start(), run(), and join() for managing threads effectively.

Uploaded by

shubham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

9 Exp Python

The document outlines an academic experiment focused on writing Python programs utilizing threading, aimed at enhancing students' understanding of multithreading concepts. It discusses the benefits of multithreading, when to use it, and provides practical examples of implementing threading in Python using the threading module. Additionally, it covers essential methods like start(), run(), and join() for managing threads effectively.

Uploaded by

shubham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

‭Mahatma Gandhi Mission's College of Engineering and Technology‬

‭Department of Computer Science and Engineering (AIML &DS)‬

‭Academic Year 2024-25(Even Sem)‬

‭Experiment No. 9‬

‭Name of Student‬ ‭Shyamdin Prajapati‬

‭Roll No‬ ‭55‬

‭DOP‬ ‭DOS‬ ‭Marks/Grade‬ ‭Signature‬

‭Aim:‬‭Write python programs on threading.‬


‭Objective:‬ ‭To study threading using python.‬
‭Outcome:‬‭Students will be able to understand threading using python.‬
‭Theory:‬
‭ ython‬ ‭Multithreading‬ ‭Multithreading‬ ‭is‬ ‭a‬ ‭threading‬ ‭technique‬ ‭in‬ ‭Python‬ ‭programming‬ ‭to‬ ‭run‬
P
‭multiple‬‭threads‬‭concurrently‬‭by‬‭rapidly‬‭switching‬‭between‬‭threads‬‭with‬‭a‬‭CPU‬‭help‬‭(called‬‭context‬
‭switching).‬ ‭Besides,‬ ‭it‬ ‭allows‬ ‭sharing‬ ‭of‬ ‭its‬ ‭data‬ ‭space‬‭with‬‭the‬‭main‬‭threads‬‭inside‬‭a‬‭process‬‭that‬
‭share‬ ‭information‬ ‭and‬ ‭communication‬ ‭with‬ ‭other‬ ‭threads‬ ‭easier‬ ‭than‬ ‭individual‬ ‭processes.‬
‭Multithreading‬ ‭aims‬‭to‬‭perform‬‭multiple‬‭tasks‬‭simultaneously,‬‭which‬‭increases‬‭performance,‬‭speed‬
‭and improves the rendering of the application.‬

‭Benefits of Multithreading in Python‬

‭Following are the benefits to create a multithreaded application in Python, as follows:‬


‭1.‬ ‭It ensures effective utilization of computer‬‭system resources.‬

‭2.‬ ‭Multithreaded applications are more responsive.‬

‭3.‬ ‭It shares resources and its state with sub-threads‬‭(child) which makes it more economical.‬

‭4.‬ ‭It makes the multiprocessor architecture more‬‭effective due to similarity.‬

‭5.‬ ‭It saves time by executing multiple threads‬‭at the same time.‬

‭6.‬ ‭The system does not require too much memory‬‭to store multiple threads.‬

‭When to use Multithreading in Python?‬


I‭ t‬ ‭is‬ ‭a‬ ‭very‬ ‭useful‬ ‭technique‬ ‭for‬ ‭time-saving‬ ‭and‬ ‭improving‬ ‭the‬ ‭performance‬ ‭of‬ ‭an‬
‭application.‬ ‭Multithreading‬ ‭allows‬ ‭the‬ ‭programmer‬ ‭to‬ ‭divide‬ ‭application‬ ‭tasks‬ ‭into‬
‭subtasks‬ ‭and‬ ‭simultaneously‬ ‭run‬ ‭them‬ ‭in‬ ‭a‬ ‭program.‬ ‭It‬ ‭allows‬ ‭threads‬ ‭to‬ ‭communicate‬
‭and‬‭share‬‭resources‬‭such‬‭as‬‭files,‬‭data,‬‭and‬‭memory‬‭to‬‭the‬‭same‬‭processor.‬‭Furthermore,‬

‭LAB MANUAL [IV --- Python Lab] Page‬‭21‬


‭Mahatma Gandhi Mission's College of Engineering and Technology‬

‭Department of Computer Science and Engineering (AIML &DS)‬

‭Academic Year 2024-25(Even Sem)‬

i‭t‬‭increases‬‭the‬‭user's‬‭responsiveness‬‭to‬‭continue‬‭running‬‭a‬‭program‬‭even‬‭if‬‭a‬‭part‬‭of‬‭the‬
‭application is the length or blocked‬
‭How to achieve multithreading in Python?‬

‭ here are two main modules of multithreading used to handle threads in‬‭Python‬
T
‭1.‬ ‭The thread module‬
‭2.‬ ‭The threading module‬

‭ hread‬‭modules‬‭It‬‭is‬‭started‬‭with‬‭Python‬‭3,‬‭designated‬‭as‬‭obsolete,‬‭and‬‭can‬‭only‬‭be‬‭accessed‬‭with‬
T
‭_thread‬‭that supports backward compatibility.‬

‭ yntax:‬‭thread.start_new_thread‬‭(‬‭function_name,‬‭args[,‬‭kwargs]‬‭)‬‭To‬‭implement‬‭the‬‭thread‬‭module‬
S
‭in‬‭Python,‬‭we‬‭need‬‭to‬‭import‬‭a‬‭thread‬‭module‬‭and‬‭then‬‭define‬‭a‬‭function‬‭that‬‭performs‬‭some‬‭action‬
‭by setting the target with a variable.‬

‭Threading Modules‬
‭ he‬ ‭threading‬ ‭module‬ ‭is‬ ‭a‬ ‭high-level‬ ‭implementation‬ ‭of‬ ‭multithreading‬ ‭used‬ ‭to‬ ‭deploy‬ ‭an‬
T
‭application‬‭in‬‭Python‬‭To‬‭use‬‭multithreading,‬‭we‬‭need‬‭to‬‭import‬‭the‬‭threading‬‭module‬‭in‬‭the‬‭Python‬
‭Program.‬

‭Methods‬ ‭Description‬

‭start()‬ ‭ ‬ ‭start()‬ ‭method‬ ‭is‬ ‭used‬ ‭to‬ ‭initiate‬ ‭the‬ ‭activity‬ ‭of‬‭a‬
A
‭thread.‬‭And‬‭it‬‭calls‬‭only‬‭once‬‭for‬‭each‬‭thread‬‭so‬‭that‬
‭the execution of the thread can begin.‬
‭run()‬ ‭ ‬ ‭run()‬ ‭method‬ ‭is‬ ‭used‬ ‭to‬ ‭define‬ ‭a‬ ‭thread's‬‭activity‬
A
‭and‬ ‭can‬ ‭be‬ ‭overridden‬ ‭by‬ ‭a‬ ‭class‬ ‭that‬ ‭extends‬ ‭the‬
‭threads class.‬
‭join()‬ ‭ ‬ ‭join()‬ ‭method‬ ‭is‬ ‭used‬ ‭to‬ ‭block‬ ‭the‬ ‭execution‬ ‭of‬
A
‭another code until the thread terminates.‬

‭o‬‭Target‬‭: It defines the function name that is executed‬‭by the thread.‬

‭o‬ ‭Args‬‭: It defines the arguments that are passed to the target function name.‬

‭For example:‬‭i‭m
‬ port‬‭threading def print_hello(n):‬
‭print("Hello, how old are you ", n)‬
‭t1 = threading.Thread( target = print_hello, args =(18, ))‬

I‭ n‬‭the‬‭above‬‭code,‬‭we‬‭invoked‬‭the‬‭print_hello()‬‭function‬‭as‬‭the‬‭target‬‭parameter.‬‭The‬‭print_hello()‬
‭contains one parameter‬‭n‬‭, which passed to the‬‭args‬‭parameter.‬
‭LAB MANUAL [IV --- Python Lab] Page‬‭22‬
‭Mahatma Gandhi Mission's College of Engineering and Technology‬

‭Department of Computer Science and Engineering (AIML &DS)‬

‭Academic Year 2024-25(Even Sem)‬

3‭ .‬ ‭Start‬ ‭a‬ ‭new‬ ‭thread:‬ ‭To‬ ‭start‬ ‭a‬ ‭thread‬‭in‬‭Python‬‭multithreading,‬‭call‬‭the‬‭thread‬‭class's‬‭object.‬


‭The‬ ‭start()‬ ‭method‬ ‭can‬ ‭be‬ ‭called‬ ‭once‬ ‭for‬ ‭each‬ ‭thread‬ ‭object;‬ ‭otherwise,‬ ‭it‬ ‭throws‬ ‭an‬ ‭exception‬
‭error.‬

‭Syntax: t1.start() t2.start()‬

4‭ .‬ ‭Join‬‭method:‬‭It‬‭is‬‭a‬‭join()‬‭method‬‭used‬‭in‬‭the‬‭thread‬‭class‬‭to‬‭halt‬‭the‬‭main‬‭thread's‬
‭execution‬ ‭and‬ ‭waits‬ ‭till‬ ‭the‬ ‭complete‬ ‭execution‬ ‭of‬ ‭the‬ ‭thread‬ ‭object.‬ ‭When‬ ‭the‬ ‭thread‬
‭object is completed, it starts the execution of the main thread in Python.‬ ‭Joinmethod.p‬‭y‬
‭import‬‭threading‬

‭def print_hello(n):‬
‭Print("Hello,‬‭how old are you? ", n)‬
‭T1 = threading.Thread(‬‭target = print_hello, args = (20, ))‬
‭T1.start()‬
‭T1.join()‬
‭Print("Thank you")‬
‭Output:‬
‭Hello, how old are you? 20‬‭Thank you‬

‭ hen‬ ‭the‬ ‭above‬ ‭program‬‭is‬‭executed,‬‭the‬‭join()‬‭method‬‭halts‬‭the‬‭execution‬‭of‬‭the‬‭main‬


W
‭thread‬ ‭and‬ ‭waits‬ ‭until‬ ‭the‬ ‭thread‬ ‭t1‬ ‭is‬ ‭completely‬ ‭executed.‬‭Once‬‭the‬‭t1‬‭is‬‭successfully‬
‭executed, the main thread starts its execution.‬
‭ ote:‬‭If‬‭we‬‭do‬‭not‬‭use‬‭the‬‭join()‬‭method,‬‭the‬‭interpreter‬‭can‬‭execute‬‭any‬
N
‭print statement‬
i‭nside‬‭the‬‭Python‬‭program.‬‭Generally,‬‭it‬‭executes‬‭the‬‭first‬‭print‬‭statement‬
‭because the‬
i‭nterpreter‬ ‭executes‬ ‭the‬ ‭lines‬ ‭of‬ ‭codes‬ ‭from‬ ‭the
‭program's start.‬
‭1.‬ ‭Synchronizing Threads in Python‬
‭It‬‭is‬‭a‬‭thread‬‭synchronization‬‭mechanism‬‭that‬‭ensures‬‭no‬‭two‬‭threads‬‭can‬‭simultaneously‬‭execute‬‭a‬
‭particular‬‭segment‬‭inside‬‭the‬‭program‬‭to‬‭access‬‭the‬‭shared‬‭resources.‬‭The‬‭situation‬‭may‬‭be‬‭termed‬‭as‬
‭critical‬‭sections.‬‭We‬‭use‬‭a‬‭race‬‭condition‬‭to‬‭avoid‬‭the‬‭critical‬‭section‬‭condition,‬‭in‬‭which‬‭two‬‭threads‬
‭do not access resources at the same time.‬

‭LAB MANUAL [IV --- Python Lab] Page‬‭23‬


‭Mahatma Gandhi Mission's College of Engineering and Technology‬

‭Department of Computer Science and Engineering (AIML &DS)‬

‭Academic Year 2024-25(Even Sem)‬

‭Source Code:‬

f‭ rom threading import Thread‬


‭import time‬

‭def display():‬
‭print("HI")‬

‭def display_hello_five_times():‬
‭for i in range(5):‬
‭t = Thread(target=display)‬
‭t.start()‬
‭t.join()‬

‭display_hello_five_times()‬

‭class myTh(Thread):‬
‭def run(self):‬
‭for i in range(1, 6):‬
‭print(i)‬

t‭1 = myTh()‬
‭t1.start()‬
‭t1.join()‬

‭class myThread:‬
‭def __init__(self, str):‬
‭self.str = str‬

‭def display(self, x, y):‬


‭print(self.str)‬
‭print("The args are", x, y)‬

o‭ bj = myThread("Bye")‬
‭t2 = Thread(target=obj.display, args=(2, 3))‬
‭t2.start()‬
‭t2.join()‬

‭def print_even():‬
‭for i in range(0, 10, 2):‬
‭print(i, end="\t")‬
‭time.sleep(0.045)‬

‭def print_odd():‬
‭for i in range(1, 10, 2):‬
‭print(i)‬
‭time.sleep(0.05)‬

p‭ rint("Even\tOdd")‬
‭thread1 = Thread(target=print_even)‬
‭thread2 = Thread(target=print_odd)‬
‭LAB MANUAL [IV --- Python Lab] Page‬‭24‬
‭Mahatma Gandhi Mission's College of Engineering and Technology‬

‭Department of Computer Science and Engineering (AIML &DS)‬

‭Academic Year 2024-25(Even Sem)‬

t‭hread1.start()‬
‭thread2.start()‬
‭thread1.join()‬
‭thread2.join()‬

‭InPut and OutPut:‬

‭Conclusion‬‭: In This way we implement Multithreading in python‬

‭LAB MANUAL [IV --- Python Lab] Page‬‭25‬

You might also like