class My_Thread(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
self.process = None

def run(self):
    print('Starting ' + self.name)
cmd = ['bash', 'process.sh']
self.process = p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=
    subprocess.STDOUT)
for line in iter(p.stdout.readline, ''):
    print('-- ' + line.rstrip())
print('Exiting ' + self.name)

def stop(self):
    print('Trying to stop thread ')
if self.process is not None:
    self.process.terminate()
self.process = None
thr = My_Thread()
thr.start()
time.sleep(30)
thr.stop()
thr.join()
How terminate Python thread without checking flag continuously