Q:

How to stop a for loop in a thread in Python?

I'm trying to create a script in Python to learn threading and I can't seem to stop the for loop in the thread. Currently, I'm compiling the script using pyInstaller and ending the Thread process which I know isn't the best way to do it, could anyone explain to me how to end a thread on command? I have read many other questions, but I cannot seem to understand how to stop a thread the 'right' way. Here is the code I am using as of now to test it out:
class Thread(Thread):
        def __init__(self, command, call_back):
        self._command = command
        self._call_back = call_back
        super(Thread, self).__init__()

    def run(self):
        self._command()
        self._call_back()
def test():
    i = 20
    for n in range(0,i):
        #This is to keep the output at a constant speed
        sleep(.5)
        print n
def thread_stop():
    procs = str(os.getpid())
    PROCNAME = 'spam.exe'
    for proc in psutil.process_iter():
        if proc.name == PROCNAME:
            text = str(proc)[19:]
            head, sep, tail = text.partition(',')
            if str(head) != procs:
                subprocess.call(['taskkill', '/PID', str(head), '/F'])

The functions are called by a GUI made in Tkinter, which is fine for now.
If you don't want to read all that, to the point: How do I stop a thread the 'right way' when there is a for loop in the thread in Python? Thanks!
EDIT: Sorry, I pulled the code I thought was most important. Instead, here's the entire code (it is a text messager which I am using to learn Python, however the above is my first try at threading before I started to undersand it). http://pastebin.com/qaPux1yR

A:

You should never forcibly kill a thread. Instead use some kind of "signal" that the thread checks at regular intervals, and if set then the thread finishes nicely.
The most simple kind of "signal" is a simple boolean variable, and can be used something like this:
class MyThread(Thread):
    def __init__(self):
        self.continue = True

    def run(self):
        while (self.continue):
            # Do usefull stuff here
            pass

    def stop(self):
        self.continue = False

