trying to kill threads
Rune Hansen
rune.hansen at viventus.no
Sat Feb 8 12:35:26 EST 2003
anton wilson wrote:
>
> I'm trying to figure out how to kill a python thread in a way that won't
> trash the interpreter or memory. I have a program that allows a user to
> use our pre-defined python functions to create threads and run his/her own
> function. But when the stop button is pressed, I want to get rid of the
> threads the user created fairly quickly.
>
> I'm sure every understands the futility of using kill() with python.
>
> Any suggestions?
>
>
> Thanks,
>
> Anton
Hi Anton, although this topic is thoroughly discussed before, it probably
won't hurt with a quick repetition. You should be using threading events..
import threading
class myThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self._stopevent = threading.Event()
def run(self):
while not self._stopevent.isSet():
"""do stuff"""
print "running..."
"""sleep one sec"""
self._stopevent.wait(1)
def join(self,timeout=None):
self._stopevent.set()
threading.Thread.join(self,timeout)
t = myThread()
t.start()
t.join()
Having your users call t.join() ensures that the thread exits nicely.
/rune
More information about the Python-list
mailing list