3

センサー値を読み取るPythonスクリプト(以下に抜粋)があります。残念ながら、一度に5〜60分間しか実行されず、その後突然停止します。これを永遠に効率的に実行する方法はありますか?このようなPythonスクリプトをRaspberryPiで永久に実行できなかった理由はありますか、それとも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()
4

2 に答える 2

1

ループに遅延がなく、常に「値」配列を追加しているように見えます。これにより、かなり短い時間でメモリが不足する可能性があります。値の配列を毎回追加することを避けるために、遅延を追加することをお勧めします。

遅延を追加する:

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)
于 2013-02-02T21:23:40.293 に答える
0

理論的には、これは永久に実行されるはずであり、Python はスクリプトの実行を自動的に制限しません。readadcまたはフィードがハングしてスクリプトがロックされたり、実行中に例外が発生したりするという問題が発生していると思いますpac(ただし、コマンドラインからスクリプトを実行すると、それが表示されるはずです)。スクリプトはハングしますか、それとも停止して終了しますか?

を使用してデータを出力print()し、Pi で表示できる場合は、簡単なデバッグ行を追加して、ハングしている場所を確認できます。タイムアウト引数を使用して簡単に修正できる場合とできない場合があります。別の方法としては、スクリプトをスレッド化し、ループ本体をスレッドとして実行し、メイン スレッドがウォッチドッグとして機能し、処理に時間がかかりすぎる場合は処理スレッドを強制終了することもできます。

于 2013-02-02T20:24:02.567 に答える