python - 如何让我的 Python 脚本永远运行?

标签 python

我有一个读取传感器值的 python 脚本(摘录如下)。不幸的是,它一次只能运行 5 - 60 分钟,然后突然停止。有没有一种方法可以有效地使它永远运行?像这样的 python 脚本为什么不能在 Raspberry Pi 上永远运行,或者 python 会自动限制脚本的持续时间有什么原因吗?

 while True:
    current_reading = readadc(current_sensor, SPICLK, SPIMOSI, SPIMISO, SPICS)
    current_sensed = (1000.0 * (0.0252 * (current_reading - 492.0))) - correction_factor

    values.append(current_sensed)
    if len(values) > 40:
           values.pop(0)

    if reading_number > 500:
           reading_number = 0

    reading_number = reading_number + 1

    if ( reading_number == 500 ):
           actual_current = round((sum(values)/len(values)), 1)

           # open up a cosm feed
           pac = eeml.datastream.Cosm(API_URL, API_KEY)

           #send data
           pac.update([eeml.Data(0, actual_current)])

           # send data to cosm
           pac.put()

最佳答案

看起来你的循环似乎没有延迟并且不断地附加你的“值”数组,这可能会导致你在相当短的时间内耗尽内存。我建议添加延迟以避免每时每刻都附加值数组。

添加延迟:

import time
while True:
    current_reading = readadc(current_sensor, SPICLK, SPIMOSI, SPIMISO, SPICS)
    current_sensed = (1000.0 * (0.0252 * (current_reading - 492.0))) - correction_factor

    values.append(current_sensed)
    if len(values) > 40:
           values.pop(0)

    if reading_number > 500:
           reading_number = 0

    reading_number = reading_number + 1

    if ( reading_number == 500 ):
           actual_current = round((sum(values)/len(values)), 1)

           # open up a cosm feed
           pac = eeml.datastream.Cosm(API_URL, API_KEY)

           #send data
           pac.update([eeml.Data(0, actual_current)])

           # send data to cosm
           pac.put()
    time.sleep(1)

关于python - 如何让我的 Python 脚本永远运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14666053/

相关文章:

python - 如何使独立编译的 cython 包使用共享随机数生成器?

python - 如何在 Django 中发送成功完成测试的电子邮件

python - 类型错误 : object of type 'Tensor' has no len() when using a custom metric in Tensorflow

python - 找不到 'cairo.Context' 的外部结构转换器

python - 如何在 Python 中使用 BeautifulSoup 分离标签?

python - 如何获得透视变形图像中点的(x,y)?

javascript - 在 JavaScript 中找不到文件

python - 根据另一列查找公共(public)列值

python - 如何使 Dijkstra 算法报告最短路线的完整最终距离

python - 用科学坐标轴作图,改变有效数字的个数