threads

V

vijayca

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...
 
D

Diez B. Roggisch

vijayca said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top