class My_Thread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
print "Starting " + self.name
cmd = [ "bash", 'process.sh']
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
for line in iter(p.stdout.readline, b''):
print ("-- " + line.rstrip())
print "Exiting " + self.name
def stop(self):
print "Trying to stop thread "
self.run = False
thr = My_Thread()
thr.start()
time.sleep(30)
thr.stop()
thr.join()
我有一个线程,就像上面展示的一样,实际上在Windows上工作,并且process.sh是在cygwin中运行的bash脚本,需要大约5分钟才能完成执行,因此它不是循环而是一些模拟过程。
我想在这个类中创建stop()函数,以便在需要时立即终止脚本。终止后,我不希望从process.sh脚本中得到任何有用的结果。
请您提供任何方法,如果可能,请稍微解释一下。