I was doing some reading(1,2,3) because I wanted to learn how to stop a thread properly.
I came up with a trivial example:
import time
import threading
def counting_numbers(stopper):
for num in range(1,20):
if not stopper.is_set():
print(num)
stopper.wait(2)
stop = threading.Event()
write = threading.Thread(target=counting_numbers, args=(stop,))
write.start()
time.sleep(5)
stop.set()
Suppose if this was a GUI application and I had a cancel button, would the stop.set()
go inside the cancel button handler allowing the user to hit cancel and stop the count?