i understand extensively covered topic, play off of answers available.
i have thread has infinite loop in run()
method. i'm implementing something similar this, there stop()
function added extended threading.thread
class.
consider thread structure:
class stoppablethread(threading.thread): def __init__(self, *args, **kwargs): super(logicthread, self).__init__(*args, **kwargs) self._stop_flag = threading.event() def run(self): while true: if self.stopped(): break; # program logic goes here def stop(self): self._stop_flag.set() def stopped(self): return self._stop_flag.isset()
this seems work expected. thread seems start , stop, given calling a.isalive();
returns false
in terminate catch
block.
but program not exit. stops executing threads (i placed print
statements inside run methods, can tell thread run()
method has been exited), program doesn't terminate. tried setting thread.daemon = true
, no avail. seems me fail safe should sys.exit()
doesn't work either.
consider main method:
import sys if __name__ == '__main__': try: logging.info('starting!') = stoppablethread() a.start() while true: time.sleep(100); except keyboardinterrupt: logging.critical('program terminating!') a.stop() a.join() logging.critical('program exited!') sys.exit()
and advice can give me fix issue appreciated. thank you!