我有一个在 Raspberry Pi 上运行的程序,我想每 15 分钟在整点的 0、15、30 和 45 分钟从温度计中提取一些数据。
我已经使用 while 循环尝试过此操作,之前我有效地使用了 time.sleep(900),但有时会偏离整点后的 0、15、30 和 45 分钟。
目前我有这个;
from datetime import datetime
def run(condition):
while condition == True:
if (datetime.now().minute == (0 or 15 or 30 or 45)):
#perform some task
temperature_store()
Run Code Online (Sandbox Code Playgroud)
为简单起见,我没有深入了解Temperature_store() 的作用,但它从插入 Pi 的传感器读取温度,然后将其打印出来。
我希望Temperature_store() 每 15 分钟发生一次,但目前,它每秒发生一次。
我知道这可能是因为我的 while 循环的逻辑/语法错误,但我无法弄清楚。(对python脚本没有太多经验,耽误时间)。