I've been playing along with threads in Python, and I came across something interesting with the following code:
import time
import threading
class Update(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.stop_event = threading.Event()
def join(self, timeout=None):
self.stop_event.set()
threading.Thread.join(self, timeout)
def run(self):
while not self.stop_event.isSet():
print("test")
thread = Update()
thread.start()
This code randomly stops even if I don't call the join() method. As a result, I get different outputs like these:
test@debian:~/$ python3 test.py
test
test
test
test
test@debian:~/$ python3 test.py
test
test
test
test
test
test
test
test@debian:~/$ python3 test.py
test
test
Why is this code randomly stopping? I thought that only by setting stop_event this thread would stop.