0% found this document useful (0 votes)
6 views10 pages

Multithreading

The document explains multithreading in Python, highlighting its role in multitasking by allowing multiple threads to execute simultaneously, which improves performance. It describes how to create threads using the threading module, including the use of the Thread class and its methods like start() and join(). An example is provided to illustrate the creation and execution of threads in a Python program.

Uploaded by

kbommidi
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)
6 views10 pages

Multithreading

The document explains multithreading in Python, highlighting its role in multitasking by allowing multiple threads to execute simultaneously, which improves performance. It describes how to create threads using the threading module, including the use of the Thread class and its methods like start() and join(). An example is provided to illustrate the creation and execution of threads in a Python program.

Uploaded by

kbommidi
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
You are on page 1/ 10

Multithreaded Programming

(or)
Multithreading in python
Multitasking
 Multitasking is a process of executing multiple tasks
simultaneously.
Example:
– Typing a python program in notepad.
– Listening lectures.
– Downloading a file from internet.
The above three tasks are performed simultaneously in a
system, but there is no dependence of one task on another task.

 Executing multiple tasks simultaneously, where each task is


separate independent part of process (or) program is called
as thread based multitasking.
 The each independent part is called as thread.
 Multitasking is used to reduce response time of system and
improves performance.
Thread based multitasking (Multithreading)
 It is a process of executing multiple threads
simultaneously.
 Multithreading allows you to break down an application
into multiple sub-tasks and run these tasks
simultaneously.
 In other words, the ability of a process to execute
multiple threads parallelly is called multithreading.
 Ideally, multithreading can significantly improve the
performance of any program.
Multithreading
Example:
Let a program has 10k line of code, where last 5k lines of code
doesn’t depend on first 5k lines of code, then both can be executed
simultaneously. It takes less time to complete the execution.
Multithreading
 A thread is a lightweight process; In simple words, a thread is a
sequence of some instructions within a program that can be
executed independently of other code.
 Threads are independent; if there is an exception in one thread it
doesn’t affect remaining threads.

 As shown in the figure, a thread is executed inside the process.


There can be multiple processes inside the OS, and each process
can have multiple threads.
Creating Thread Using Threading Module
• In Python, threading module provides Thread class. To create a
new thread, we create an object of the Thread class.
Syntax:
threading.Thread (target=None, args=())

This constructor has the following arguments:


• target is the function to be executed by the thread. Defaults to
None, meaning nothing is called.
• args is the arguments to be passed to the target function. Value
should be list or tuple of arguments (x,y,…). Defaults to ()

(should give more than one arg or None ex: args=(10, ) )


Threading Module in Python
• The threading module provides features and support for
threads . This Module provides Thread class.
• Thread class provide following methods:
• start() − The start() method starts a thread for execution.

• join( timeout=None ) − The join() waits for threads to


terminate.
This blocks the calling thread until the thread whose join()
method is called terminates – either normally or through an
unhandled exception – or until the optional timeout occurs.
(stops execution of the main program until the thread is
completed)
Example:
import threading
#function display
def display(msg):
for i in range(5):
print(msg)
# creating thread
t1 = threading.Thread(target=display, args=("Thread1", ))
t2 = threading.Thread(target=display, args=("Thread2", ))
# starting thread 1
t1.start()
# starting thread 2
t2.start()
# wait until thread 1 is completely executed
t1.join()
#main prog. is waiting for t1 to finish
# wait until thread 2 is completely executed
t2.join()
# both threads completely executed
print("Done!")
Example
Let us try to understand the above code:
• To import the threading module, we do:
import threading
• To create a new thread, we create an object of Thread class. It
takes following arguments:
target: the function to be executed by thread
args: the arguments to be passed to the target function
• In above example, we created 2 threads with different
arguments passed to the target function:
t1 = threading.Thread(target=display, args=(“Thread1”,))
t2 = threading.Thread(target=display, args=(“Thread2”,))
Example
• To start a thread, we use start method of
Thread class.
t1.start()
t2.start()
• Once the threads start, the current program also keeps on
executing. In order to stop execution of current program
until a thread is complete, we use join method.
t1.join()
t2.join()
• As a result, the current program will first wait for the
completion of t1 and then t2.
• Once, they are finished, the remaining statements of current
program are executed.

You might also like