kill/cancel a started thread

ellisjb at my-deja.com ellisjb at my-deja.com
Wed Aug 25 09:16:33 EDT 2004


You need to have your thread check for some condition that signals it
to exit.  E.g., stopnicely in the following class (subclass and provide
an action method to do something useful):

class StoppableThread(threading.Thread):
def __init__(self, **kwds):
threading.Thread.__init__(self, **kwds)
self.keepgoing = True

def stopnicely(self):
self.keepgoing = False

def run(self):
while self.keepgoing:
time.sleep(1)
self.action()
print "Thread " + self.getName() + " has stopped"

-Jonathan




More information about the Python-list mailing list