Free cookie consent management tool by TermsFeed The notify() method of the Condition class in Python | Pythontic.com

The notify() method of the Condition class in Python

Overview:

  • In Python, the notify() method of the Condition class is used to notify other threads that are blocking for a predicate to become True.
  • Before calling notify() the calling thread has to acquire the lock associated with the condition variable.
  • The caller must release the underlying lock by calling release() after calling the notify() method on a Condition object.
  • The notify() method by default wakes up one thread that is in blocked state using the same condition variable. By specifying a value greater than one notify() can be made wake up a specific number of waiting threads.

Example:

Though two consumer threads are notfied upon filling of the deque the consumers proceed one after another as the same lock is used by both the threads. 

# Example Python program that uses the notify() method 
# of Condition object to notify the other threads waiting 
# on the same condition object
import threading as th
import time
import collections as coll

# A deque. It is filled by the producer
# thread and emptied by consumer threads
jobQueue = coll.deque(maxlen = 10)

# Target function for the producer thread
def produce(cv):
    global jobQueue
    while(len(jobQueue) <= 0):
        cv.acquire()
        for i in range(1, jobQueue.maxlen + 1):
            jobQueue.append("Job:%d"%i)
        cv.notify(2)
        cv.release()
        print("Producer has notified the consumers")
        # Give some time for consumers to act upon
        # If the sleep time is less producer terminates
        # after one round of production 
        # and other means are required to keep the
        # while loop running
        time.sleep(.01) 
    print("Producer quiting")

# Target function for the consumer thread
def consume(start_pos, end_pos, cv):
    global jobQueue

    while(True):
        cv.acquire()
        cv.wait()
        for i in range(start_pos, end_pos):
            job = jobQueue.pop()
            tid = th.get_ident()
            print("time:%s thread_id:%s job_id:%s"%(time.time(), tid, job))

        cv.release()

# Create the lock object
lock = th.Lock()

# Create the condition object 
cvar = th.Condition(lock)

# Create the producer thread
pThread     = th.Thread(target = produce, args = (cvar,))

start     = 0
end     = jobQueue.maxlen/2 

# Create the consumer threads
cThread1     = th.Thread(target = consume, args = (int(start), int(end), cvar))

start     = start + end 
end     = jobQueue.maxlen
cThread2     = th.Thread(target = consume, args = (int(start), int(end), cvar))

# Start all the threads
cThread1.start()
cThread2.start()
pThread.start()

# Wait for child threads to complete
pThread.join()
cThread1.join()
cThread2.join()

Output:

Producer has notified the consumers
time:1757924039.893707 thread_id:6109425664 job_id:Job:10
time:1757924039.893742 thread_id:6109425664 job_id:Job:9
time:1757924039.893748 thread_id:6109425664 job_id:Job:8
time:1757924039.8937511 thread_id:6109425664 job_id:Job:7
time:1757924039.893754 thread_id:6109425664 job_id:Job:6
time:1757924039.8937652 thread_id:6126252032 job_id:Job:5
time:1757924039.89378 thread_id:6126252032 job_id:Job:4
time:1757924039.893782 thread_id:6126252032 job_id:Job:3
time:1757924039.893785 thread_id:6126252032 job_id:Job:2
time:1757924039.893787 thread_id:6126252032 job_id:Job:1

 


Copyright 2025 © pythontic.com