import threading
import time
def cold_temp():
# Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before.
tfile = open("/sys/bus/w1/devices/28-021571bf69ff/w1_slave")
# Read all of the text in the file.
text = tfile.read()
# Close the file now that the text has been read.
tfile.close()
# Split the text with new lines (\n) and select the second line.
secondline = text.split("\n")[1]
# Split the line into words, referring to the spaces, and select the 10th word (counting from 0).
temperaturedata = secondline.split(" ")[9]
# The first two characters are "t=", so get rid of those and convert the temperature from a string to a number.
temperature = float(temperaturedata[2:])
# Put the decimal point in the right place and display it.
temperature = temperature / 1000
return temperature
output = cold_temp()
f = open('/var/www/html/coldtemp.html', 'w')
print >> f, output
f.close()
cold_temp()
我已尝试过上述和简单的
def cold_temp():
while True:
# Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before.
tfile = open("/sys/bus/w1/devices/28-021571bf69ff/w1_slave")
# Read all of the text in the file.
text = tfile.read()
# Close the file now that the text has been read.
tfile.close()
# Split the text with new lines (\n) and select the second line.
secondline = text.split("\n")[1]
# Split the line into words, referring to the spaces, and select the 10th word (counting from 0).
temperaturedata = secondline.split(" ")[9]
# The first two characters are "t=", so get rid of those and convert the temperature from a string to a number.
temperature = float(temperaturedata[2:])
# Put the decimal point in the right place and display it.
temperature = temperature / 1000
return temperature
output = cold_temp()
f = open('/var/www/html/coldtemp.html', 'w')
print >> f, output
f.close()
time.sleep(1)
我想每秒运行一次脚本。以上两个都运行一次然后结束。
答案 0 :(得分:2)
您可以使用sched
模块安排重复运行的内容,无论是每两小时还是每一秒。
如果您的功能运行时间超过一秒(不太可能只检查温度),那么您将有延迟。
你也会看到一些"漂移"在时间上由于你的功能运行所花费的时间,因为它反复安排。所有微小的"位"你的功能运行所需的时间最终会加起来。
e.g。 -
import sched, time
s = sched.scheduler(time.time, time.sleep)
def some_function():
print time.time()
s.enter(1, 0, some_function)
s.enter(1, 0, some_function)
s.run()
答案 1 :(得分:2)
您的while循环位置错误。我冒昧地改变你的代码来使用with
子句,这是打开文件的更标准的方法,但如果你有一个非常古老的python,它将会中断。
import threading
import time
def cold_temp():
# Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before.
with open("/sys/bus/w1/devices/28-021571bf69ff/w1_slave") as tfile:
# skip first line, keep second line
next(tfile)
secondline = next(tfile)
# Split the line into words, referring to the spaces, and select the 10th word (counting from 0).
temperaturedata = secondline.split(" ")[9]
# The first two characters are "t=", so get rid of those and convert the temperature from a string to a number.
temperature = float(temperaturedata[2:])
# Put the decimal point in the right place and display it.
temperature = temperature / 1000
return temperature
while True:
output = cold_temp()
with open('/var/www/html/coldtemp.html', 'w') as f:
print >> f, output
time.sleep(1)
答案 2 :(得分:1)
第一个运行一次,因为你没有给它任何重复运行的方法。第二个命中return
,它甚至在有机会输出任何东西之前退出函数(从而退出循环)。移除return
,然后使用temperature
获取output
的值。