2016-04-30 46 views
0
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() 

我已經試過以上和簡單Python中沒有運行每秒

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) 

我想運行腳本的每一秒。上述兩個運行一次然後結束。

+1

究竟是什麼問題? – TehSphinX

+3

究竟發生了什麼?腳本崩潰了嗎?它每1.1秒運行一次還是不全? –

+0

向我們展示'做點什麼'。 –

回答

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) 
+0

謝謝!這工作完美。決定將溫度計放入我的嘴裏,然後冰冷的水中進行測試。大 –

2

您可以使用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() 
+0

我不能得到這個工作,但謝謝。 –

+0

's.enter'還需要5個參數 –

1

第一個運行一次,因爲你沒有給它任何反覆運行的方式。第二個命中return,在它甚至有機會輸出任何東西之前退出函數(並因此退出循環)。刪除return,只需使用temperature的值爲output

+0

不會輸出錯誤,但不會將輸出寫入html文件 –

+0

不要調用'output = cold_temp()'。它是遞歸:'cold_temp'不斷調用自己永遠不會到達輸出部分。只需打印「溫度」的值。 –

+0

仍然不寫.. '溫度=溫度/ 1000 F =開放( '/無功/網絡/ HTML/coldtemp.html', 'W') 打印>> F,溫度 f.close( ) time.sleep(1)' –