【发布时间】:2021-11-02 06:06:03
【问题描述】:
我有一个应用程序,我正在尝试每分钟添加网站更新。这部分目前工作得很好。我在当前这段代码摘录中遇到的问题是,当我关闭/退出应用程序时,我必须按几次“X”按钮,它会完全崩溃并死机。
如果我的理解是正确的,我相信这种情况正在发生,因为当我尝试退出时,time.sleep() 仍在不断“运行”。
如何运行这样的定期更新,当我想关闭它时不会让应用程序陷入困境?有人可以在这里帮我解决吗?
我在这里的这个工作示例中只添加了 5 秒的睡眠时间,而不是我预期的 60 秒,以便在您测试时节省时间。
import time
from threading import Thread
from kivy.app import App
class Test(App):
def build(self):
self.thread_for_update()
def thread_for_update(self):
p1 = Thread(target=lambda: self.check_for_update())
p1.start()
def check_for_update(self):
time.sleep(5)
print("Update")
# Here I'm checking an online data source for changes
# I will also notify user if theres changes
self.thread_for_update()
Test().run()
【问题讨论】:
标签: python multithreading function time sleep