Python Multi - Threading Introduction

The term multithreading is associated with a terminology, that is multitasking. So in this context first we have to know about the concept of multitasking. Executing multiple tasks simultaneously is known as multitasking. There are two types of multitasking like process-based multitasking and thread-based multitasking.


Process-based multitasking

Executing multiple tasks simultaneously where each task is a separate independent process is known as process based multi-tasking.

For example; while writing a java program in the editor, at the same time we can listen music through MP3 player, and at the same time we can download a file from the internet. All these tasks are executing simultaneously and independent of each other. So it is called as process based multi-tasking.

It is best suitable at operating system level.

Disadvantages
  • • Each process contain its own memory area.
  • • That's why it is a heavy weight process.
  • • Cost communication between process is high.

Thread-based multitasking

Thread based multi-tasking is called as multithreading. Executing multiple tasks simultaneously where each task is a separate independent part of same program is known as thread based multi-tasking. It is best suitable at programmatic level.

Now we will know what exactly a thread is !!!

  • • A thread is a sequence of instructions in a program that can be executed independently of the remaining program.
  • • Many threads can run concurrently within a program.
  • • At runtime threads in a program exist in a common memory space and can, therefore, share both data and code, that is they are lightweight compared to processes.
  • • They also share the process running the program

Note:

To create a thread we have to import a package that is from threading import *


from threading import *
class MyThread(Thread): 
	def run(self):
		for k in range(5):
			print("\n Python Programming")

obj=MyThread()        #Thread Instance 
obj.start()

for x in range(5):
	print("\n JAVA Programming")

Output:

Python Programming
Python Programming
Python Programming
Python Programming
Python Programming
JAVA Programming
JAVA Programming
JAVA Programming
JAVA Programming
JAVA Programming


class A(Thread):
	def run(self):
		for k in range(5):
			print("\n Python")

class B(Thread):
	def run(self):
		for j in range(5):
			print("\n Python for AI")

ob1=A()      #Thread Instance 
ob2=B()

ob1.start() 
ob2.start()

Output:

Python
Python
Python
Python
Python
Python for AI
Python for AI
Python for AI
Python for AI
Python for AI


def f1():
	print("Python Programming") 

obj=Thread(target=f1)
obj.start()
print("\n SILAN Software")

Output:

Python Programming
SILAN Software


print(current_thread().getName

Output:

MainThread


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext