Signup/Sign In

Thread Synchronization using Event Object

It's time to learn more about threads in python. In this tutorial we will be covering an important classe, the Event class which is used for thread synchronization in python.

This class is used for inter thread communication by generating events.


Python Multithreading: Event Object

The Event class object provides a simple mechanism which is used for communication between threads where one thread signals an event while the other threads wait for it. So, when one thread which is intended to produce the signal produces it, then the waiting thread gets activated.

An internal flag is used by the event object known as the event flag which can be set as true using the set() method and it can be reset to false using the clear() method.

The wait() method blocks a thread until the event flag for which it is waiting is set true by any other thread..

Following are the useful functions used along with an event object:

Initialize Event object

We can initialize an Event object as follows:

import threading

are_you_coming = threading.Event()

When we initialize an event object like this the internal flag is set to false by default.


isSet() method

This method returns true if and only if the internal flag is true.

import threading

are_you_coming = threading.Event()
print(are_you_coming.isSet())

False


set() method

When this method is called for any event object then the internal flag is set to true. And as soon as set() methos is called for any event all threads waiting for it are awakened.


clear() method

This method resets the internal flag to false. Subsequently, threads calling wait() on the event for which clear() is called, it will block until the internal flag is not true again.


wait([timeout]) method

When we have to make any thread wait for an event, we can do so by calling this method on that event which has the internal flag set to false, doing so blocks the thread until the internal flag is true for the event.

If the internal flag is true on entry, then the thread will never get blocked. Otherwise, it is blocked until another thread calls set() to set the flag to true, or until the optional timeout occurs. The timeout argument specifies a timeout for the operation in seconds.


Time for an Example

Let's have a simple code example to demonstrate the usage of Event class object.

In the code below we will create a thread and make it wait for an event which will be generated by the main thread, releasing the first thread.

In the above program we have also used the timeout property of the wait() method.

When a thread calls wait([timeout]) method, the method returns a boolean value true if the wait is released on receiving the event object, else it returns false if the wait is released due to timeout.

To test this, change the value of timeout sent as argument args=(e,4) on line 18 and keep it less than the sleep time, for example set the timeout value to 2 and see the output.