... continued

Continuation of discussion on condition variables.

We'll cover the following...

In the previous sections, we worked with wait() and notify() methods. These methods have closely related cousins which take in parameters. We discuss them as follows:

wait(n)

The wait(n) method takes in a floating point parameter n. This is the number of seconds a calling thread would wait to be notified by another thread. The wait method times out after n seconds and the thread is woken up even if no notification is received. Consider the below snippet:

Using wait(n)

from threading import Condition
from threading import Thread
import time

flag = False

cond_var = Condition()


def child_task():
    cond_var.acquire()

    if (flag == False):
        cond_var.wait(1)

    if (flag == False):
        print("child thread times out waiting for a notification")

    # don't forget to release the lock    
    cond_var.release()


thread = Thread(target=child_task)
thread.start()

time.sleep(3)
thread.join()

Note that we have digressed from the idiomatic usage of the wait method by not testing for the condition in a while ...