python - Running Python function at defined intervals
问题描述
I've got a Python loop that should run every minute, do some data processing and sleep until the next minute is up. However the processing takes variable amount of time, sometimes it's close to zero when there is not much to do, sometimes it takes even 10 or 20 seconds.
To compensate for that I measure the time it takes to run the processing like this:
while True:
time_start = time.time()
do_something() # <-- This takes unknown time
time_spent = time.time() - time_start
time.sleep(60 - time_spent)
It kind of works but over a couple of days it still drifts away by a number of seconds. I guess it happens when the computer (small Raspberry Pi) is busy and delays the start of the loop, then it all starts slipping away.
I don't need do_something()
executed exactly every minute, so no need for real-time OS or anything like that, but I don't want one delayed start affect all the subsequent ones either.
Is there some kind of scheduler that can run my function at a predefined rate? Or some more clever way to compensate for the occasional delays?
解决方案
Playing with the loop a little this seems to work quite well. The trick is to record the start time once before the loop starts, not on every iteration. That way one delayed start won't affect any future ones.
rate_sec = 60
time_start = time.time()
while True:
print("{}".format(datetime.now()))
# Emulate processing time 0s to 20s
time.sleep(random.randint(0, 20))
# Sleep until the next 'rate_sec' multiple
delay = rate_sec - (time.time() - time_start) % rate_sec
time.sleep(delay)
推荐阅读
- javascript - ASP.NET 核心发布项目 - JavaScript 无法在所有设备上运行
- c# - 如何使用 ILoggerFactory 从 Polly 记录重试
- java - 如何避免很多 if else 条件
- ios - 使用扩展重构代码时出现“Swift 键路径中的预期表达式”错误
- javascript - 跨屏幕传递数据
- python - 如何在所有子图上获取标签和轴标题(python)
- python - 未正确调用 DataFrame 构造函数
- c# - 绕过视图在 MVC 中调用控制器操作
- visual-studio - 当解决方案中存在网站项目时,在 Visual Studio 中构建 DockerCompose 项目会出错
- java - 在 JBoss 6.4 上部署时 Spring Boot 应用程序挂起