trying to kill threads

Rune Hansen rune.hansen at
Sat Feb 8 18:35:26 CET 2003

anton wilson wrote:

> I'm trying to figure out how to kill a python thread in a way that won't
> trash the interpreter or memory. I have a program that allows a user to
> use our pre-defined python functions to create threads and run his/her own
> function. But when the stop button is pressed, I want to get rid of the
> threads the user created fairly quickly.
> I'm sure every understands the futility of using kill() with python.
> Any suggestions?
> Thanks,
> Anton
Hi Anton, although this topic is thoroughly discussed before, it probably 
won't hurt with a quick repetition. You should be using threading events..

import threading
class myThread(threading.Thread):
        def __init__(self):
                self._stopevent = threading.Event()

        def run(self):
                while not self._stopevent.isSet():
                        """do stuff"""
                        print "running..."
                        """sleep one sec"""

        def join(self,timeout=None):

t = myThread()

Having your users call t.join() ensures that the thread exits nicely.


More information about the Python-list mailing list