1

私は、ファイルの行を毎分1回カウントし、いくつかの計算を行い、最後の1分間に追加された行数を計算するように構築されたpythonデーモンを持っています。出力は、カウントとタイムスタンプを含む基本的な csv であるため、後で別のスクリプトでグラフ化できます。

デーモンは一定時間実行され、「1 ~ 1 時間か 2 時間のようです」と終了します。理由については示されていません。

基本的なデーモン テンプレートは、次の記事から派生しています。

import os
import subprocess
import time
from daemon import runner
import datetime

inputfilename="test_data"

class App():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path =  '/tmp/counter.pid'
        self.pidfile_timeout = 5
    def run(self):

        counter1 = 0
        counter_log = 0
        while True:

            count = 0
            output = open("tpm_counter.log", 'a')            

            FILEIN = open(inputfilename, 'rb')
            while 1:
              buffer = FILEIN.read(8192*1024)
              if not buffer: break
              count += buffer.count('\n')
            FILEIN.close(  )

            counter_log =  (count - counter1) * 30

            now = str(datetime.datetime.now())

            counter1 = count

            line = str(counter_log)
            output.write(line)
            output.write(", ")
            output.write(now)
            output.write("\n")

            output.close( )

            # set the sleep time for repeated action here:
            time.sleep(60)

app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()

なぜこれが終了するのか、誰にも分かりますか?

4

1 に答える 1

2

例外以外に、ループが終了する理由が 1 つあります。それが次の行です。

if not buffer: break

中断する前にこの行をログに記録し、そこからトレースを開始することをお勧めします。

更新
コードをもう少し詳しく見ていたところ、内側のループを見落としていました。外側のループを壊すものは何もないので、例外に違いないと思います。非常に簡単な提案は、ループ全体を try-except でラップし、発生したものをすべてログに記録することです。

try:
    while True:
        ...
except Exception, e:
    # log e
    raise
于 2012-05-20T04:27:20.987 に答える