[Tutor] how to stop a thread

Kent Johnson kent37 at tds.net
Thu Jan 4 16:08:05 CET 2007


shawn bright wrote:
> hello there all,
> 
> i have a python reference book that got me started knowing how to use 
> threads, i can declare one as a class, and start it like this
> 
> class SomeThread(threading.Thread):
> 
> 
> run_process = SomeThread()
> run_process.start()
> 
> but how do i kill it ? This part is not in my book. Basically the thread 
> works, and runs in a while loop. Is there a way to destroy the instance 
> of it after creation ?

A thread dies when it's run() method exits, so you have to contrive a 
way to make run() exit. The usual way is to set a flag that the thread 
checks. Here is an example using a threading.Event as a flag:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65448

Kent



More information about the Tutor mailing list