Python Event Class | 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

is_set() is an inbuilt method of the Event class of the threading module in Python.

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:

    from threading import Event

Syntax:

    is_set()

Parameter(s):

  • None

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:

# 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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.