www.edureka.co/python
What is Multitasking in Python?
What is a thread?
How to achieve Multithreading in Python?
When to use Multithreading in Python?
How to create Threads in Python?
• Without creating a class
• By extending Thread class
• Without extending Thread class
Advantages of Multithreading
Types of Multitasking
What is Multitasking in Python? www.edureka.co/python
www.edureka.co/python
What is
Multitasking in
Python?
Multitasking refers to the ability of an operating system to perform
different tasks at the same time
Types of Multitasking www.edureka.co/python
www.edureka.co/python
Types of
Multitasking
Process Based
Multiple threads
running on the same
OS simultaneously.
Example:
Downloading, listening
to songs and playing a
game..
01
Thread Based
Single process
consisting of
separate tasks.
Example: A game of
FIFA consists of
various threads.
.
02
There are two types of multitasking in an OS:
www.edureka.co/python
What is a Thread? www.edureka.co/python
www.edureka.co/python
What is a Thread?
Thread 1 Thread2
Main Code
Thread 3 … Thread n
How to Achieve Multithreading in Python?www.edureka.co/python
Multithreading in Python can be achieved by importing the threading module.
www.edureka.co
How to Achieve Multithreading in Python
conda install -c conda-forge tbb
Importing
importthreading
fromthreadingimport*
www.edureka.co/python
When to use Multithreading in Python?www.edureka.co/python
www.edureka.co/python
When to use
Multithreading
in Python?
Multithreading in Python can be used
when:
Multiple tasks need to achieved
Tasks do not have interdependency.
How to Create Threads in Python? www.edureka.co/python
www.edureka.co/python
How to Create Threads in Python?
01 Without creating a class
02By extending Thread class
03 Without extending Thread class
www.edureka.co/python
Without creating
a class
from threading import *
print(current_thread().getName())
def mt():
print("Child Thread")
child=Thread(target=mt)
child.start()
print("Executing thread name
:",current_thread().getName())
www.edureka.co
By extending Thread class
import threading
import time
class mythread(threading.Thread):
def run(self):
for x in range(7):
print("Hi from child")
a = mythread()
a.start()
time.sleep(1)
print("bye",current_thread().getName
())
www.edureka.co/python
www.edureka.co/python
Without
extending Thread
class
from threading import *
class ex:
def myfunc(self):
for x in range(7):
print("Child")
myobj=ex()
thread1=Thread(target=myobj.myfunc)
thread1.start()
#threading.active_count()
thread1.join()
thread1.join()
print("done")
www.edureka.co/python
Advantages of Multithreading www.edureka.co/python
Advantages of
Multithreading
Enhanced performance by decreased development time
Simplified and streamlined program coding
Simultaneous and parallelized occurrence of tasks
Better use of CPU resource
www.edureka.co/python
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
www.edureka.co
www.edureka.co/python

What is Multithreading In Python | Python Multithreading Tutorial | Edureka