Python Event Class | wait() Method with Example

Python Event.wait() Method: Here, we are going to learn about the wait() method of Event Class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 22, 2020

Python Event.wait() Method

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

When we want a thread to wait for an event, we can call the wait() method on that event whose internal flag is set to false, thereby blocking the thread until the internal flag of that event object is set to true by the set() method. If the internal flag is true on entry, then the thread is not blocked. The thread remains blocked until the internal flag remains false or the timeout occurs. We can give the optional timeout to the function with the help of the timeout argument.

Module:

    from threading import Event

Syntax:

    wait(timeout=None)

Parameter(s):

  • timeout: It is an optional parameter, which specifies the timeout for the wait() method. Once this value exceeds, the threads, waiting on the event, get unblocked. Its default value is None.

Return value:

The return type of this method is <class 'Bool'>. It returns if the thread gets released before the timeout else returns false.

Example 1:

# Python program to explain the
# use of wait() method in Event() class
import threading
import time

def helper_function(event_obj, timeout,i):
  # Thread has started, but it will wait 10 seconds for the event  
  print("Thread started, for the event to set")
 
  flag = event_obj.wait(timeout)
  if flag:
    print("Event was set to true() earlier, 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,10,27))
  thread1.start()
  # sleeping the current thread for 5 seconds
  time.sleep(5)
  
  # generating the event
  event_obj.set()
  print("Event is set to true. Now threads can be released.")
  print()

Output:

Thread started, for the event to set
Event is set to true. Now threads can be released.

Event was set to true() earlier, moving ahead with the thread

Example 2:

# Python program to explain the
# use of wait() method in Event() class
import threading
import time

def helper_function(event_obj, timeout,i):
  # Thread has started, but it will wait 3 seconds for the event  
  print("Thread started, for the event to set")
 
  flag = event_obj.wait(timeout)
  if flag:
    print("Event was set to true() earlier, 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,3,27))
  thread1.start()
  # sleeping the current thread for 5 seconds
  time.sleep(5)
  
  # generating the event
  event_obj.set()
  print("Event is set to true. Now threads can be released.")
  print()

Output:

Thread started, for the event to set
Time out occured, event internal flag still false. Executing thread without waiting for event
Value to be printed= 27
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.