threads

Diez B. Roggisch deets at nospam.web.de
Wed Sep 5 14:59:55 CEST 2007


vijayca wrote:

> i have written a script that spawns some threads....
> all run in parallel....
> now i need to stop all the threads once i press a key(example: "\n")
> how to do it...

That's a FAQ. Search this NG/ML for exhaustive discussions. The quick answer
is:

 - you can't stop externally

 - so you must rely on frequently checking some flag inside your worker
threads.

Like this (untested):

class MyThread(Thread):
  
  def __init__(self):
     Thread.__init__(self)
     self.running = True

  def run(self):
     while self.running:
         work()

Set MyThread.running to False when the key is pressed.


Diez



More information about the Python-list mailing list