How to terminate Thread in Python?

The safest way is using threading.Event to avoid sync problem.

import threading
 
class TestThread(threading.Thread):
 
    def __init__(self):
        self.stopEvent = threading.Event()
 
    def run(self):
        while not self.stopEvent.isSet():
            pass
 
    def stop(self):
        self.stopEvent.set()

Leave a comment

Please be polite and on topic. Your e-mail will never be published.