我有一个读取传感器值的 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/