Because someone else will set it.
You generally start a thread in one part of your application and continue to do whatever you do:
thread = TimerClass()
thread.start()
# Do your stuff
The thread does it's stuff, while you do your stuff. If you want to terminate the thread you just call:
thread.event.set()
And the thread will stop.
So the answer is: event, in this case, is not used for controlling the thread from inside the thread object itself. It is used for controlling the thread from outside (from the object which holds the reference to the thread).
Answer from Viktor Kerkez on Stack OverflowEquivalent of Python threading.Event for sync Rust
multithreading - Python threading: will Event.set() really notify every waiting thread - Stack Overflow
Python Threading Tutorial: Basic to Advanced (Multithreading, Pool Executors, Daemon, Lock, Events)
Can someone ELI5 threading?
A program runs a bunch of instructions. For example, "add 2 and 5", and "print the words 'Hello world' to the console". Normally, these would happen one after the other. First, the program would perform the addition and then would print to the console.
We can use "threads" to do multiple things at once. Our program would create a separate thread that would do nothing but "add 2 and 5" and would then create another thread that would do nothing but "print the words 'Hello world' to the console". These threads can run at the same time, rather than in a predetermined order. The program would be adding and printing at the same time.
This comes with a few drawbacks. We have some data, lets say an integer with a value of 1. When we use this data in our first thread, we change the integers value to 2. In our second thread we are going to be printing that same integer. We run our program about 1000 times. Sometimes, our program prints 1 and sometimes it prints 2. This is because sometimes one thread is quicker at it's job than the other. If thread one is quicker than thread two, then we will see 2 printed out. If thread 2 is quicker than thread 1, then we will see 1 printed it.
Hopefully this is helpful. I added the bit about the drawbacks to give you a bit more knowledge than requested.
More on reddit.comVideos
In the internals of Python, an event is implemented with a Condition() object.
When calling the event.set() method, the notify_all() of the condition is called (after getting the lock to be sure to be not interrupted), then all the threads receive the notification (the lock is released only when all the threads are notified), so you can be sure that all the threads will effectively be notified.
Now, clearing the event just after the notification is not a problem.... until you do not want to check the event value in the waiting threads with an event.is_set(), but you only need this kind of check if you were waiting with a timeout.
Examples :
pseudocode that works :
#in main thread
event = Event()
thread1(event)
thread2(event)
...
event.set()
event.clear()
#in thread code
...
event.wait()
#do the stuff
pseudocode that may not work :
#in main thread
event = Event()
thread1(event)
thread2(event)
...
event.set()
event.clear()
#in thread code
...
while not event.is_set():
event.wait(timeout_value)
#do the stuff
Edited : in python >= 2.7 you can still wait for an event with a timeout and be sure of the state of the event :
event_state = event.wait(timeout)
while not event_state:
event_state = event.wait(timeout)
It's easy enough to verify that things work as expected (Note: this is Python 2 code, which will need adapting for Python 3):
import threading
e = threading.Event()
threads = []
def runner():
tname = threading.current_thread().name
print 'Thread waiting for event: %s' % tname
e.wait()
print 'Thread got event: %s' % tname
for t in range(100):
t = threading.Thread(target=runner)
threads.append(t)
t.start()
raw_input('Press enter to set and clear the event:')
e.set()
e.clear()
for t in threads:
t.join()
print 'All done.'
If you run the above script and it terminates, all should be well :-) Notice that a hundred threads are waiting for the event to be set; it's set and cleared straight away; all threads should see this and should terminate (though not in any definite order, and the "All done" can be printed anywhere after the "Press enter" prompt, not just at the very end.
Are you trying to make your code run faster? In this video, we will be taking a deep dive into python threads from basic to advanced concepts so that you can take advantage of parallelism and concurrency to speed up your program.
-
Python Thread without join()
-
Python Thread with join()
-
Python Thread with Input Arguments
-
Python Multithreading
-
Python Daemon Threads
-
Python Thread with Synchronization using Locks
-
Python Thread Queue Communication between Threads
-
Python Thread Pool Executor
-
Python Thread Events
-
Speed Comparison I/O Task
-
Speed Comparison CPU Task (Multithreading vs Multiprocessing)
https://youtu.be/Rm9Pic2rpAQ