Single Tasking and Multitasking using Python threads

Single Tasking and Multitasking using Python Threads

This chapter will discuss Single Tasking and Multitasking using Python threads.

Single Tasking using Threads

We can set a thread to execute only one job at a time. If there are three tasks, we can make each thread get executed one after the other. This way of running the threads one after the other is called Single Tasking using Threads.

As mentioned below, let’s develop a plan to make rice in three steps.

  1. Wash rice with water for 2 minutes. This process represents job 1.
  2. Add adequate water and boil for 15 minutes. This process represents job 2
  3. Serve the rice to everyone at the dining table. This process represents job 3, and it takes 5 minutes for this job.

Let us now write the program that can accomplish these jobs using Single Tasking using Python threads.

#Python program to show the concept of Single Tasking using threads

#import the threading module
from threading import *
from time import sleep

#create a class
class MakeRice:
  #add a method that executes three jobs to make rice
  def howtomakeRice(self):
    self.job1()
    self.job2()
    self.job3()
  
  def job1(self):
    print("Wash Rice with water for 5 minutes...",end='')
    sleep(5)
    print("Done")
  
  def job2(self):
    print("Add adequate water and boil for 15 minutes..",end='')
    sleep(7)
    print("Done")
  
  def job3(self):
    print("Serve the rice to everyone on the dining table...",end='')
    sleep(3)
    print('Done')

#create an object for the class
instnce = MakeRice()

#create a thread and provide howtomakeRice as the target method
#to run the three jobs

t = Thread(target=instnce.howtomakeRice)

#start the thread
t.start()
Output:

Wash Rice with water for 5 minutes...Done
Add adequate water and boil for 15 minutes..Done
Serve the rice to everyone on the dining table...Done

In the above program, we imported the threading module to implement the usage of threads in our program. We created a class, MakeRice and added a method within it called howtomakeRice(), which calls other three methods in the same class – job1(), job2(), and job3(). After creating an object for the MakeRice() class, we provided the instnce.howtmakeRice as the target method for the thread to execute each job one after the other.

You can notice in your machine that there would be a delay in displaying each print statement due to the sleep() function from the time module. The sleep(5) means that we will delay the time taken for executing the following statement in the program by 5 seconds. This program is an excellent and simple example of how to use Single Tasking using threads in Python.

Multitasking using several threads

Unlike Single Tasking, Multitasking executes many threads simultaneously. To illustrate the same, we would require more than one thread. The execution of more than one thread simultaneously represents Multitasking using Python threads.

Suppose you arrived at a restaurant and want to book a table based on availability. For this purpose, you might talk to the receptionist. While he deals with your advance payment booking procedures, the restaurant’s waiter might arrange the table at the same time based on your requirements. Although different, these two tasks are working for the exact cause: your satisfaction and happiness. You can consider this a real-life example of Multitasking.

#Python program to show the concept of Multitasking using threads

#import the threading module
from threading import *
from time import sleep

class Restaurant:
  #constructor to accept a string
  def __init__(self,str):
    self.str = str

  #a method that repeats for five seats
  def bookseats(self):
    for i in range(5):
      print(self.str," : ",i+1)
      sleep(0.1)

#create two objects to Restaurant class
object1 = Restaurant('Make payment for the seat')
object2 = Restaurant('Waiter finds the seat')

#create two threads to run bookseats()
t1 = Thread(target=object1.bookseats)
t2 = Thread(target=object2.bookseats)

#run the threads
t1.start()
t2.start()
Output:

Make payment for the seat  :  1
Waiter finds the seat  :  1
Waiter finds the seat  :  2
Make payment for the seat  :  2
Waiter finds the seat  :  3
Make payment for the seat  :  3
Make payment for the seat  :  4
Waiter finds the seat  :  4
Make payment for the seat  :  5
Waiter finds the seat  :  5

You can see in the program above that we executed two separate threads simultaneously. We ran the thread related to the payment for the seat and the thread associated with finding the seat at the same time for each member.

Scroll to Top