我想要触发upfunction
,并在文件名中写入3时停止。基本上我想在条件满足时停止线程,如下所示。条件匹配时停止线程
def getstatus():
fh = open(filename,'r')
return fh.read()
def upfunction(arg):
for i in range(arg):
print ("backup running")
print(getstatus())
target = open(filename, 'w')
target.write(str(i))
sleep(1)
if __name__ == "__main__":
thread = Thread(target = upfunction, args = (10,))
thread.start()
print(getstatus())
while getstatus() != "3":
print("NOT 3 ")
sleep(0.5)
continue
thread.stop()
print("thread finished...exiting")
它显示
AttributeError: 'Thread' object has no attribute 'stop'
请把我当成新手到Python。 任何帮助将高度赞赏
'threading.Event'已经存在。 – erip
@erip我同意你,但对于新手我的答案更容易理解,恕我直言 –