Suppose inside run() method of a python Thread , I check a flag.If that flag is True , I assume my thread should exit has done it’s job and should exit.
How should I exit the thread at that point ? Trying Thread.exit()
class workingThread(Thread):
def __init__(self, flag):
Thread.__init__(self)
self.myName = Thread.getName(self)
self.FLAG= flag
self.start() # start the thread
def run(self) : # Where I check the flag and run the actual code
# STOP
if (self.FLAG == True):
# none of following works all throw exceptions
self.exit()
self._Thread__stop()
self._Thread_delete()
self.quit()
# RUN
elif (self.FLAG == False) :
print str(self.myName)+ " is running."
korylprince is correct. You just need a return statement, or in your case pass:
Since you have no looping structure in the code, the thread is going to terminate in both cases. Basically once the function returns the thread will exit. Add a loop of some kind in there if you want do more than one operation.