Open In App

Inter Thread Communication With Condition() Method in Python

This article is based on how we use the Condition() method to implement Inter Thread Communication, let’s discuss this topic below -:

Let’s have some brief discussion on Inter Thread Communication before talking about Condition() implementation for inter-thread communication, When any thread required something from another they will organize communication between them, and with this communication, they will fulfill their requirement. It means that it is a process of communicating between the threads for any kind of requirement. 



Condition Method

We can say that the implementation of inter-thread communication using the condition() method is the up-gradation of that event object used for inter-thread communication. Here Condition represents some type of state change between threads like ‘send notification’, ‘got notification’.  See below how the condition object is creating:-     

Syntax :



condition_object = threading.condition( )

In this scenario, threads can wait for that condition and once that condition executes then threads can modify according to that condition. In simple word, we can say that the condition object gave access to threads to wait until another thread give notification to them. Condition object is always correlated with lock (RLock) concept internally.

Some following methods of Condition class we discuss below :

  1. release()
  2. acquire()
  3. notify()
  4. wait()
  5. notifyAll()

 

Syntax: condition_object.release()
Syntax: condition_object.acquire()
Syntax :condition_object.notify()                                      
Syntax: condition_object.wait()
Parameter:Time 
Syntax: condition_object.notifyAll()

Now let’s take a simple example to show how acquire and release methods are used and how they work through the entire program -:

Example 1:




# code
# import modules
 
import threading
import time
 
obj1= threading.Condition()
 
 
def task ():
  obj1.acquire()
  print('addition of 1 to 10 ')
  add= 0
  for i in range ( 1 , 11 ):
    add = add+i
  print(add)
  obj1.release()
  print('the condition object is releases now')
   
 
t1 = threading.Thread(target = task)
t1.start()

Output
addition of 1 to 10 
55
the condition object is releases now

In the above example, we use acquire() and release() method which can be used to obtain or acquire the condition object and after the completion of tasks releases the condition object. First, we are importing the required modules, and then we create the condition object which is obj1 then we create and start the thread t1, In the task named function there where we used acquire() method from these we obtain the condition object and also from here internal lock of threads are started. After the completion of instructions at the last we release the condition object and also the internal lock of threads is released.

Example 2:

In this example we used other methods to explain how they work through the entire program:-




# code
 
# import modules
 
import time
from threading import *
import random
 
 
 
class appointment:
   
  def patient(self):
    condition_object.acquire()
    print('patient john waiting for appointment')
    condition_object.wait()   # Thread is in waiting state
    print('successfully got the appointment')
    condition_object.release()
 
  def doctor(self):
    condition_object.acquire()
    print('doctor jarry checking the time for appointment')
    time=0
    time=random.randint(1,13)
    print('time checked')
    print('appointed time is {} PM'.format(time))
    condition_object.notify()
    condition_object.release()
 
   
condition_object = Condition()
class_obj=appointment()
 
T1 = Thread(target=class_obj.patient)
 
T2 = Thread(target=class_obj.doctor)
 
T1.start()
 
T2.start()

Output
patient john waiting for appointment
doctor jarry checking the time for appointment
time checked
appointed time is 3 PM
successfully got the appointment

This is a simple example to explain the use of Condition() class and their methods in threading. Also, here we use an example of patient and doctor how patient and doctor first acquire the conditions and then how to notify them, and at last both also release the condition object. Let’s start first we create a class appointment which was having two functions which name as a patient() and doctor() and then we created two threads T1 and T2. With the help of the acquire() method, the doctor and patient both acquire the condition object in their first statement by this both the threads are internally locked. Now the patient has to wait for an appointment and got appointed after the execution of the wait() method doctor starting to check whether the time he chooses is it ok to go for an appointment or not and when the time is chosen by the doctor then it will notify to thread that is in waiting for the state by notify() method. Next after the notify() method, the patient got their notification and product both. Then after all this, both threads release the condition object by release() method and internal lock releases. 


Article Tags :