[英]Python not running every second
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)
我想每秒運行一次腳本。 以上兩種運行一次,然后結束。
您可以使用sched
模塊來安排某些內容重復運行,無論是每兩個小時還是每秒一次。
如果您的函數運行時間超過一秒(不太可能僅用於檢查溫度),則會有延遲。
由於重復執行功能需要花費時間,因此您還會看到一些“漂移”。 您的函數運行所花費的所有微小的“時間”最終都會累加起來。
例如-
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()
您的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)
第一個運行一次,因為您沒有給它任何重復運行的方式。 第二個return
一個return
,該return
在函數甚至沒有輸出任何內容之前就退出了該函數(並因此退出了循環)。 刪除return
,僅將temperature
用作output
值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.