Event Object in Python

  • January 6, 2023
  • python
JOIN Clause
What is an Event Object?

An Event object in Python is a class that is used to signal the occurrence of a particular event. It is part of the threading module in Python and is used to synchronize threads.

It is used to manage an internal flag that can be set to True using the set() method and reset to False using the clear() method. The wait() method can be called on the event object to block the execution of the thread until the flag is set to True.

Functions used with an Event object:

Here are some useful functions that can be used with an Event object in Python:

  • is_set(): This method returns True if the internal flag of the event is set to True, and False otherwise.
  • set(): This method sets the internal flag of the event to True, causing any threads that are waiting for the event to be set to continue executing.
  • clear(): This method resets the internal flag of the event to False, causing any threads that are waiting for the event to be set to block until the event is set again.
  • wait([timeout]): This method blocks the calling thread until the internal flag of the event is set to True or until the optional timeout argument has expired. If the timeout argument is given and the internal flag is not set to True before the timeout expires, the method returns False. If the timeout argument is not given, the method blocks indefinitely until the event is set.
  • set() and clear() can be used to signal the occurrence of an event, while wait() can be used to block the execution of a thread until the event occurs.
Example:
import threading

# Create an event object
event = threading.Event()

# Create a function that will be executed by a thread
def function_1():
    # Wait for the event to be set
    event.wait()
    print("Event is set")

# Create a thread and pass the function to be executed
thread = threading.Thread(target=function_1)

# Start the thread
thread.start()

# Set the event
event.set()

# Clear the event
event.clear()

# Wait for the event to be set again
event.wait()
print("Event is set again")

In this example, the function_1 function will be executed by the thread “thread”. The event.wait() line will cause the thread to block until the event is set using the event.set() line. When the event is set, the thread will continue executing and will print “Event is set”.

The event is then cleared using the event.clear() method, which will cause the flag to be reset to False. The event.wait() line will then cause the main thread to block until the event is set again using the event.set() line. When the event is set again, the main thread will continue executing and will print “Event is set again”.

Note: also read about RLock Object in Python

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Reply

Your email address will not be published. Required fields are marked *