首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python:"while“太慢了,在实际执行中睡眠(0.25)变成了睡眠(3)。

Python:"while“太慢了,在实际执行中睡眠(0.25)变成了睡眠(3)。
EN

Stack Overflow用户
提问于 2016-09-27 07:57:11
回答 1查看 1.1K关注 0票数 1

我正在运行一个Python程序在一个树莓Pi 3,我想记录温度从一个DS18B20传感器每0.25秒。

早些时候,当程序很简单,并且在外壳上显示温度时,它是相当快的,没有问题。不幸的是,由于程序本身,其中包括登录到一个文件,我得到一个日志每2秒或3秒只。

如何确保0.25秒的日志记录间隔。

我分享了以下代码:

代码语言:javascript
复制
#This program logs temperature from DS18B20 and records it
#Plots the temperature-time plot.

import os
import sys
#import matplotlib.pyplot as plt
from re import findall
from time import sleep, strftime, time
from datetime import *

#plt.ion()
#x = []
#y = []

ds18b20 = ''

def setup():
    global ds18b20
    for i in os.listdir('/sys/bus/w1/devices'):
        if i != 'w1_bus_master1':
            ds18b20 = i

# Reads temperature data from the Temp sensor
# This needs to be modified for use with max31855 and K-type thermocouples
def read():
#   global ds18b20
    location = '/sys/bus/w1/devices/' + ds18b20 + '/w1_slave'
    tfile = open(location)
    text = tfile.read()
    tfile.close()
    secondline = text.split("\n")[1]
    temperaturedata = secondline.split(" ")[9]
    temperature = float(temperaturedata[2:])
    temperature = temperature / 1000
    return temperature

#Loop for logging - sleep, and interrupt to be configured.
def loop():
        while True:
                if read() != None:
                        print "Current temperature : %0.3f C" % read()
                        #sleep(0.25)
                        func()

def write_temp(temperature,file_name):
        with open(file_name, 'a') as log:
                log.write("{0},{1}\n".format(datetime.now().strftime("%d-%m-%Y %H:%M:%S"),str(temperature)))

arg = sys.argv[1]
filename1 = str(arg) + "-" + datetime.now().strftime("%d-%m-%Y-%H-%M-%S")+".csv"

def func():
        temperature = read()
        #sleep(0.25)
        write_temp(temperature,filename1)
        #graph(temperature)

#For plotting graph using MatPlotLib
#Comment out this function during foundry trials to avoid system slowdown
#Check system resource usage and slowdown using TOP or HTOP

#def graph(temperature):
#        y.append(temperature)
#        x.append(time())
#        plt.clf()
#        plt.scatter(x,y)
#        plt.plot(x,y)
#        plt.draw()              

#Interrupt from command-line
def destroy():
    pass

if __name__ == '__main__':
    try:
        setup()
        func()
        loop()
    except KeyboardInterrupt:
        destroy()

我已经注释掉了一些我认为资源很重的部分,但是我仍然不能管理少于2秒的任何内容。我得到的结果如下:

输出:

代码语言:javascript
复制
27-09-2016 12:18:41,23.0
27-09-2016 12:18:43,23.062
27-09-2016 12:18:46,23.125
27-09-2016 12:18:48,23.187
27-09-2016 12:18:50,23.187
27-09-2016 12:18:53,23.562
27-09-2016 12:18:55,25.875
27-09-2016 12:18:58,27.187
27-09-2016 12:19:00,27.5
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-27 08:13:39

  1. 只打开日志文件一次(并在程序退出时关闭)
  2. 不要总是从传感器上重新读取温度。你打电话给read()太频繁了。
  3. 减少一般开销,并简化您的呼叫。

我不能完全测试这一点,但这样的东西应该能用:

代码语言:javascript
复制
import os
import sys
import time
from datetime import datetime

def read_temp(dev):
    '''Reads temperature from sensor and returns it as float.'''
    loc = '/sys/bus/w1/devices/' + dev + '/w1_slave'
    with open(loc) as tf:
        return float(tf.read().split('\n')[1].split(' ')[9][2:]) / 1000.0

def write_temp(t, logfile):
    '''Writes temperature as .3 float to open file handle.'''
    logfile.write('{0},{1:.3f}\n'.format(datetime.now().strftime('%d-%m-%Y %H:%M:%S'), t))

def loop(dev, logfile):
    '''Starts temperature logging until user interrupts.'''
    while True:
        t = read_temp(dev)
        if t:
            write_temp(t, logfile)
            print('Current temperature: {0:.3f} °C'.format(t))
            sys.stdout.flush() # Flush. Btw, print is time-consuming!
            time.sleep(.25)

if __name__ == '__main__':
    # Take the first match for a device that is not 'w1_bus_master1'
    dev = [d for d in os.listdir('/sys/bus/w1/devices') if d != 'w1_bus_master1'][0]
    # Prepare the log filename
    fname = str(sys.argv[1]) + "-" + datetime.now().strftime("%d-%m-%Y-%H-%M-%S")+".csv"
    # Immediately open the log in append mode and do not close it!
    logfile = open(fname, 'a')

    try:
        # Only pass device and file handle, not the file name.
        loop(dev, logfile)
    except KeyboardInterrupt:
        # Close log file on exit
        logfile.close()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39719177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档