Home »
Python »
Python Reference »
Python Event Class
Python Event is_set() Method with Example
Python Event.is_set() Method: Here, we are going to learn about the is_set() method of Event Class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 22, 2020
Python Event.is_set() Method
The Event.is_set() is an inbuilt method of the Event class of the threading module. An event class object manages an internal flag whose value can be changed by other methods of this class. is_set() function returns true if the internal flag of an event object is true else returns false.
Module
The following module is required to use is_set() method:
import threading
Class
The following class is required to use is_set() method:
from threading import Event
Syntax
The following is the syntax of is_set() method:
is_set()
Parameter(s)
The following are the parameter(s):
Return Value
The return type of this method is <class 'bool'>. It returns true if the internal flag of the current event class object is true else returns false.
Example of Event.is_set() Method in Python
# Python program to explain the
# use of is_set() method in Event() class
import threading
import time
def helper_function(event_obj, timeout, i):
# Thread has started, but it will wait 4 seconds
# for the event
print("Thread started, for the event to set")
print("Is the event set to true now?", event_obj.is_set())
flag = event_obj.wait(timeout)
if flag:
print("Event has set to true(), moving ahead with the thread")
else:
print("Time out occured, event internal flag still false. Executing thread without waiting for event")
print("Value to be printed=", i)
if __name__ == '__main__':
# Initialising an event object
event_obj = threading.Event()
# starting the thread who will wait for the event
thread1 = threading.Thread(target=helper_function, args=(event_obj, 4, 30))
thread1.start()
# sleeping the current thread for 5 seconds
time.sleep(5)
# generating the event
event_obj.set()
print("Is the event set to true now?", event_obj.is_set())
print("Event is set to true. Now threads can be released.")
Output
Thread started, for the event to set
Is the event set to true now? False
Time out occured, event internal flag still false. Executing thread without waiting for event
Value to be printed= 30
Is the event set to true now? True
Event is set to true. Now threads can be released.