【发布时间】:2020-03-22 12:22:11
【问题描述】:
我想每 5 分钟运行一次函数并让它与时钟同步。如果我使用 time.sleep(60*5),时间开始漂移,因为我的函数增加了一点处理时间。这是运行与时钟同步的函数的好方法还是在 python 中有更好的方法?
def run(condition):
def task():
#run data here
pass
runOnce = True
while condition:
if dt.datetime.now().minute % 5 == 0 and dt.datetime.now().second == 0 and runOnce:
runOnce = False
task()
elif dt.datetime.now().second != 0 and not runOnce:
runOnce = True
else:
time.sleep(0.5)
run(True)
【问题讨论】:
-
如果您使用的是 Mac/Linux,请使用
cron。
标签: python time delay clock modulo