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

Michael P. Reilly arcege at gmail.com
Wed Aug 31 02:42:08 CEST 2005


On 8/29/05, Hans Dushanthakumar <Hans.Dushanthakumar at navman.com> wrote:
> 
> 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
> 

You would probably want to use a shared threading.Condition object. A number 
of threads manuals mention using this in a boss shutdown model.
-Arcege
-- 
There's so many different worlds,
So many different suns.
And we have just one world,
But we live in different ones.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050830/f1c3ec64/attachment.html


More information about the Tutor mailing list