I have a module called randomstuff
which I import into my main program. The problem is that at times, the code being run in randomstuff
needs to be stopped, without affecting the main program.
I have tried exit(), quit() and a few os functions, but all of them want to close my main program as well. Inside of the module, I have a thread that checks if the module should be stopped - so what function should I put in the thread when it realizes that the program must be stopped.
Any ideas on how to go about this? Thanks
I have a new answer as I now know what you mean. You'll have to extend the thread class with a Boolean to store the current state of the thread like so:
mythread.py
from threading import *
import time
class MyThread(Thread):
def __init__(self):
self.running = True
Thread.__init__(self)
def stop(self):
self.running = False
def run(self):
while self.running:
print 'Doing something'
time.sleep(1.0/60)
thread = MyThread()
thread.start()
mainscript.py
from mythread import *
thread.stop() # omit this line to continue the thread