Author - StudySection Post Views - 212 views
python

Multithreading in Python

Multitasking refers to the ability of an operating system to perform different tasks at the same time, for example downloading the file while listening to music and concurrently playing the game. Thread concept is used to get a multithreading concept, first, we need to understand what is a thread?
Multitasking can be either process-based or thread-based.
In Process-Based multitasking, multiple threads are executing simultaneously on the same OS. Example: Downloading, listening to songs, and playing a game.
Thread Based multitasking is a single process that consists of separate tasks. Example: A game of FIFA consists of various threads.

Thread A smallest unit of multithreading is a thread. Threads archives separate tasks for the same process. It’s an independent flow of execution. A single process consists of multiple threads. Each thread in a program performs a particular task. For example, A particular game is a single process that consists of several threads like playing musing, takes input from the user, running components, etc.

Multithreading in python can be in two scenarios:

  • Multiple tasks need to achieved
  • A task does not have any interdependency

In python to use the multithreading have to import the threading module
import threading
from threading import *

To import this module you have to install it on the anaconda platform by using :
Conda install –c conda-forge tbb

How to create a thread in python

  • Without creating class
  • By Extending Thread Class
  • Without extending Thread class

Without Creating a class

Every process has one thread that is always executing which is a ‘main’ thread. Defining a thread using the ‘Thread’ class that exists in the threading module is explained below.
from threading import *
def my_thread():
for x in range(6):
print(“my thread”)
t1=Thread(target= my_thread)
t1.start()
print(“finished”)

t1=Thread(target= my_thread) creates the new thread and specifies the target of this child thread that is the ‘my_thread’ function and t1.start() starts the execution of the child thread t1.

multithreading python1

As we can see the final statement is printed with the help of the ‘main’ thread without waiting for the completion of the execution of the child thread that is my_thread. If the user wants the main thread to wait for the child thread to finish, we can use t1.join(). We can use the current_thread.getName() method to see which statement is called by the child thread and the main thread. ‘main’ thread is a thread that creates the child thread as in the program we print the current thread before initiating the child thread.
from threading import *
def my_thread():
for x in range(6):
print("my thread..",current_thread().getName())
print(current_thread().getName())
t1=Thread(target= my_thread)
t1.start()
t1.join()
print("finished", current_thread().getName())

multithreading python2

In the output, it is very clear that the main thread lies all the control before the process begins and Thread28 is the child thread also the final statement is executed by the main thread.

By Extending Thread Class

When you extend the thread class to create a thread you can overwrite only two functions that are run() and init() and for every python function that is defined within the class, the self parameter has to be specified.

import threading
from threading import *
class A(threading.Thread):
def run(self):
for x in range(6):
print("child thread..",current_thread().getName())
obj=A()
obj.start()
obj.join()
print("controlled return to ",current_thread().getName())

python3

Without Extending the Thread Class

from threading import *
class A:
def my_thread(self):
lst=[2,1,'w',66,'g','abc'] for x in lst:
print("child printing",x)
my_obj=A()
t1=Thread(target=my_obj.my_thread())
t1.start()
t1.join()
print("done")

python4

Advantages of Multithreading

  • Performance enhancement by decreasing the development time
  • Simplified and streamlined program coding
  • Allows simultaneous and parallelized execution of tasks
  • Better use of CPU resources

Get certification for your knowledge in the fundamentals of Computer functioning by clearing the Computer Certification Exam conducted by StudySection. After going through this Computer Certification exam, you will be able to evaluate your basic knowledge of computers.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri