2016-04-30 48 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)' –