需要在系统时钟达到特定时间时开始Python函数
我写了一段Python代码,用来定期从一个1-wire的达拉斯总线读取温度,这个总线上有三个温度传感器。这个1-wire总线连接在树莓派的GPIO引脚上。我可以通过在一个循环中运行代码,并使用time.sleep(60)这个函数来控制测量的频率。time.sleep(60)的意思是每60秒读取一次温度。
不过这样并不是最理想的。我希望每半小时整点读取一次温度,也就是在每个半小时时,通过将下一个计算的测量时间与系统时钟的实际时间进行比较。
我写了两部分代码。
..1,读取传感器温度的部分。
#!/usr/bin/env python
import time
def TakeTemperatureReadings():
datafile = open("temperaturedata.log", "a", 1)
timestamp = time.strftime("%d/%m/%Y %H:%M:%S")
# sensor on pi itself
tfile = open("/sys/bus/w1/devices/28-0000052c29e1/w1_slave")
text = tfile.read()
tfile.close()
temperature_data = text.split()[-1]
temperature = float(temperature_data[2:])
temperature1 = round(temperature / 1000,1)
# sensor half way along bus
tfile = open("/sys/bus/w1/devices/28-0000052c33ef/w1_slave")
text = tfile.read()
tfile.close()
temperature_data = text.split()[-1]
temperature = float(temperature_data[2:])
temperature2 = round(temperature / 1000,1)
# sensor at end of line
tfile = open("/sys/bus/w1/devices/28-0000052c6da7/w1_slave")
text = tfile.read()
tfile.close()
temperature_data = text.split()[-1]
temperature = float(temperature_data[2:])
temperature3 = round(temperature / 1000,1)
data_stream = str(temperature1) + ", on-pi, " + str(temperature2) + ", window, " + str(temperature3) + ", outside, " + str(timestamp)
print data_stream
datafile.write(data_stream + "\n")
# datafile.write(str(temperature1) + ", " + str(temperature2) + ", " + str(temperature3) + ", " + str(timestamp)+ "\n")
datafile.close()
time.sleep(60)
while True:
TakeTemperatureReadings()
..2,比较时间的部分。
#!/usr/bin/env python
import datetime
tt = datetime.datetime.now()
print "\n"
#print tt
print str("year NOW:") + "\t"+ str(tt.year)
print str("month NOW:") + "\t" + str(tt.month)
print str("day NOW:") + "\t" + str(tt.day)
print str("hour NOW:") + "\t" + str(tt.hour)
print "\n"
print str("minute NOW:") + "\t" + str(tt.minute)
print "\n"
actual = datetime.datetime.now()
actual = actual.replace(second=0,microsecond=0)
print str("actual Now:") + "\t" + str(actual)
print "\n"
# round to nearest 10 mins
# sd = int(round(tt.minute/10)*10)
# print 'minute rounded:', sd
# round to nearest half hour
if (int(round(tt.minute/30))) < 1:
sd=0
else:
sd=30
print '1/2 hour rounded down:', sd
# round to nearest hour
# sd = int(round(tt.minute/1000))
# print 'hour rounded:', sd
# to nearest 10 mins
# z = tt.replace(minute=10+sd,second=0,microsecond=0)
# round to nearest half hour
if sd == 0:
z = tt.replace(minute=30,second=0,microsecond=0)
elif sd == 1 and tt.hour <> 23:
z = tt.replace(hour=tt.hour+1,minute=0,second=0,microsecond=0)
else:
z = tt.replace(day=tt.day+1,hour=0,minute=0,second=0,microsecond=0)
# to nearest hour
#z = tt.replace(hour=tt.hour+1,minute=0+sd,second=0,microsecond=0)
print "\n"
print str("rounded time:") + "\t" + str(z)
print str("rounded hour:") + "\t" + str(z.hour)
print str("rounded minute:") + "\t" + str(z.minute)
print "\n"
print 'z > actual:', z > actual
print "\n"
#while
print tt.minute
print z.minute
我不知道怎么持续读取系统时钟,并将其与下一个计算的时间进行比较。我在谷歌上花了很长时间寻找答案,但还是没有找到。
任何帮助都非常感谢 :)
下面的两部分代码都能按预期工作。
2 个回答
0
像Linux这样的类UNIX系统,比如运行在树莓派上的Linux,有一些机制可以定期运行程序。
如果你想要重复执行一个命令,可以使用cron
。你可以查看 cron(8)
、crontab(1)
和 crontab(5)
的手册页面来了解更多信息。
一个每三十分钟运行一次你脚本的crontab命令可能看起来是这样的:
0,30 * * * * python /yourdir/yourscript
2
sample_time = time.time()
while true:
TakeTemperatureReadings()
sample_time += 60 * 30
while time.time() < sample_time:
time.sleep(1)
time.sleep(1) 这一行可以让你的程序在等待时不占用太多 CPU,这样会让采样变得随机,误差大约在一秒内。如果这个误差对你来说有问题,可以改成 sleep(0.1)。
sample_time = time.time()
sample_time -= sample_time % 60 * 30
sample_time += 60 * 30
while true:
while time.time() < sample_time:
time.sleep(1)
TakeTemperatureReadings()
sample_time += 60 * 30