Event
When using Threads there are situations when we want the thread to wait for some time and then continue execution. We can achieve this with the use of the Event object. An event is signaled from one thread while another waits for the event to be signaled. One of the aims of using an Event is for the synchronization of threads.
Event Object
The Event Object has an internal flag that if True does a particular task. It is set to True using the set() function and can be reset to False using the clear() function. The is_set() function checks if the internal flag is True or not.
The wait() function is used to make a thread wait till the internal flag is set to True. We are also provided with a ‘timeout’ parameter for the wait() function which we can use to determine how long the thread should wait before the thread continues execution without the event being triggered.
Let us now create a program that uses Events. The aim is to build a program that adds to a number if an event is triggered and displays it. If the event is not triggered then it simply displays the number. The triggering of the event is determined by the user input and we prompt the user two times.
Consider the following code:
import threading event_variable = threading.Event() def event_function(): print("--Thread has started--") user_number = 15 # check internal flag if event_variable.is_set(): # internal flag is true user_number += 10 print("Begin event to add 10 to user number") print("The value is:", user_number) else: # internal flag is false print("Deny event to add 10 to user number") print("The value is:", user_number) print("") # prompt event twice for x in range(2): user_input = input("Start event? Enter y/n: ") # checking user input if user_input == "y": event_variable.set() # set flag to true elif user_input == "n": event_variable.clear() # set flag to false object_ = threading.Thread(target=event_function, name="Event Thread") object_.start()
If we run this we get:
Let’s break down the program we wrote; we created the following:
- An object of the class Event.
- A function called “event_function()” starts our thread and also does what is required when the event is triggered {adds 10 and prints} and when it is not triggered {only prints}.
- A for loop that runs twice.
- A variable to store the user input.
- An if statement to begin the event or not based on the user input.
- A thread with our function as the target and gave it a name.
The for loop asks the user input two times, then the thread starts, and based on the input the event can either trigger or not trigger using the set() and clear() functions respectively. If the input is ‘y’ the flag is now True, the if condition is satisfied, and the program adds 10 to the number and displays it. If the input is ‘n’ no the flag is set to False, the if condition is not satisfied and the program simply displays our number.
Timer
If we want to run a thread only after a certain time has passed, we can use an object of the Timer class. We can use a Timer class to execute some code at some scheduled time. Similar to how we start a thread, we also start a Timer using the start() function.
Timer Object
The syntax of the Timer object is:
- threading.Timer(interval, function)
- interval – The time Python should wait before it calls the function.
- function – The name of the function we want to call.
The Timer class has two functions we can use. Firstly, the start() function begins the timer and then calls this required function when the time is up. Secondly, the cancel() function stops the timer and halts the execution of the function, however, keep in mind that this will only work when the timer has not yet been completed.
import threading def timer_function(): print("This is the timer function") object_1 = threading.Timer(interval=5, function=timer_function) print("Beginning timer to call function") object_1.start() print("Phew! Cancelled on time") object_1.cancel()
When we run this, we get:
What have we learned?
- How can we make a thread wait for a certain while?
- What are some functions of the Event object?
- How does the internal flag control the flow of events?
- What is the use of the Timer object?
- When is the cancel() function of the Timer class rendered pointless?