我想要觸發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我同意你,但對於新手我的答案更容易理解,恕我直言 –