[Tutor] Killing a thread from main - was RE: "Lock"ing threads
Hans Dushanthakumar
Hans.Dushanthakumar at navman.com
Tue Aug 30 04:17:52 CEST 2005
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