[Tutor] Killing a thread from main - was RE: "Lock"ing threads

Hans Dushanthakumar Hans.Dushanthakumar at navman.com
Tue Aug 30 04:27:55 CEST 2005


 
Oops - the error was probably due to using 'stop' as a method name.
:)


-----Original Message-----
From: Hans Dushanthakumar 
Sent: Tuesday, 30 August 2005 2:18 p.m.
To: tutor at python.org
Subject: Killing a thread from main - was RE: [Tutor] "Lock"ing threads

Thanks Kent

How do I send a signal from the main thread to stop execution of a child
thread?
   
I tried the foll:, but got an error:
Other than by this method, is there any other mechanism to stop a
thread?

import threading
import time

class shownum(threading.Thread):
    
    def __init__(self, start_num):
        threading.Thread.__init__(self)
        self.num = start_num
        self.stop = 0
        
    def run(self):
        for i in range(12):
            time.sleep(1)
            print "shownum: ", self.num
            self.num = self.num + 1
            if self.stop == 1:
                break            

    def stop(self):
        self.stop = 1

    def chng(self):
        self.num = 1
        
incr_num_thread = shownum1(201)
incr_num_thread.start()

time.sleep(3)
incr_num_thread.chng()
time.sleep(3)
incr_num_thread.stop()


Output:

shownum:  201
shownum:  202
shownum:  1
shownum:  2
shownum:  3
Traceback (most recent call last):
  File "H:\Docs\PyScripts\test_threads.py", line 31, in ?
    incr_num_thread.stop()
TypeError: 'int' object is not callable
shownum:  4
shownum:  5
shownum:  6



More information about the Tutor mailing list