In this tutorial, we will learn thread synchronization in python. In Python, the Event class is used for thread synchronization. It is basically used for inter-thread communication by generating events.
The Event class object operates with a simple mechanism to communicate between threads. This mechanism involves one thread signaling an event while the other threads wait for it. After the thread expected to produce the signal finally produces it, the waiting thread becomes activated.
An event flag is an internal flag used by the event object and it can be set as true using the set() method. It can as well be reset to false using the clear () method.
A thread can be blocked using the wait() method until the event flag for which it is waiting is set true by any other thread...
Below are the relevant functions used along with an event object:
An Event object can be initialized as follows:
import threading
are_you_coming = threading.Event()
When an event object is initialized like this the internal flag by default is set to false.
In python multithreading, isSet() method returns true if and only if the internal flag is true.
import threading
are_you_coming = threading.Event()
print(are_you_coming.isSet())
Output:
False
An internal flag is also known as an event flag is set to true when the set() method is called for any event object. The immediately set() method is called for any event all waiting threads will become activated.
An internal flag is also known as an event flag is set to true when the set() method is called for any event object. The immediately set() method is called for any event all waiting threads will become activated.
When any thread is needed to wait for an event, the wait([timeout]) method can be called on such event provided the internal flag is set to false, and the thread remains blocked until the internal flag is set to true for the event.
However, the thread will never get blocked if the internal flag is true on entry. Otherwise, it remains blocked unless another thread calls the set() method to set the flag to true, or until the optional timeout occurs. The timeout argument explicitly states a timeout for the operation in seconds.
Here is a simple code example to demonstrate the usage of the Event class object.
In the code below a thread will be created and allowed to wait for an event that will be generated by the main thread, hence releasing the first thread.
import threading
import time
def task(event, timeout):
print("Started thread but waiting for event...")
# make the thread wait for event with timeout set
event_set = event.wait(timeout)
if event_set:
print("Event received, releasing thread...")
else:
print("Time out, moving ahead without event...")
if __name__ == '__main__':
# initializing the event object
e = threading.Event()
# starting the thread
thread1 = threading.Thread(name='Event-Blocking-Thread', target=task, args=(e,4))
thread1.start()
# sleeping the main thread for 3 seconds
time.sleep(3)
# generating the event
e.set()
print("Event is set.")
Output:
Started thread but waiting for event...
Event is set.
Event received, releasing thread...
In the above program, the timeout property of the wait() method has been used.
When a thread calls the wait([timeout]) method, the wait is released on receiving the event object if the method returns a boolean value true, but it returns false if the wait is released due to timeout.
To evaluate this, replace the value of timeout sent as an argument to args=(e,4) on line 18 and keep it less than the sleep time, for minstance, set the timeout value to 2 and see the output.