0

システム ログを読み取るスクリプトを作成していますが、デーモン化するときに問題が発生しています。ランダムにハングアップし、その理由を一生理解できません。コードは次のとおりです。

デーモン

daemon_context = daemon.DaemonContext(files_preserve=[fh.stream])
with daemon_context:
    logger.debug("In Daemon")
    main()

主要

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler("./debug.log")
logger.addHandler(fh)
logger.debug("First")

def main():
    while True:
        from schema import parsed, engine, session
        logger.debug("Main Loop")
        logger.debug(args)

        if args.filename is None:
            logger.debug("In If")
            with open("/absolute/path/to/log.log") as f:
                readlines = f.readlines()
                logger.debug(readlines)
            content = [x.split() for x in readlines]
            logger.debug(content)
        else:
            logger.debug("In Else")
            with open(args.filename) as f:
                readlines = f.readlines()
                logger.debug(readlines)
            content = [x.split() for x in readlines]
            logger.debug(content)

        logger.debug("After Ifs")

        conn = engine.connect()
        logger.debug(conn)
        rows = session.query(parsed).count()

        for entry in range(rows, len(content)):
        # Code inside this for loop is unimportant to the problem at hand

ロギング

エラーがキャッチされている場所を把握するために、すべてをログに記録しています。

で実行すると、次のpython3 test.py --filename access.logようになります。

First
In Daemon
Main Loop
Namespace(filename='access.log')
In Else

それがログ全体です。止まるだけです。

しかしpython3 test.py、ファイル名の引数なしで を実行すると、次のログが取得されます。

First
In Daemon
Main Loop
Namespace(filename=None)
In If
[]
[]
After Ifs
<sqlalchemy.engine.base.Connection object at 0x7fe11c1da5f8>

その後、タスクを停止するまで無限に繰り返されます。

なぜ止まっているのか、さっぱりわかりません。誰かが私を助けることができれば、それは素晴らしいことです. 使用する前に、このスクリプトの別の形式を使用python-daemonしていましたが、それは機能していました。したがって、根底にあるロジックが健全であるべきであることはわかっています。

4

1 に答える 1