私はシステムログを読むためのスクリプトを書いています。私はそれをデーモン化する際に問題に遭遇しています。それは無作為に掛けられていて、なぜ私が私の人生のために理解することはできません。私は、エラーがキャッチされている場所を把握するために行くように私はすべてのログを記録しています Pythonデーモンが捕まえられました
ログイン
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
daemon_context = daemon.DaemonContext(files_preserve=[fh.stream])
with daemon_context:
logger.debug("In Daemon")
main()
メイン
デーモン:ここでは、コードです。
私はpython3 test.py --filename access.log
でそれを実行すると、私が手に:全体のログです
First
In Daemon
Main Loop
Namespace(filename='access.log')
In Else
。それはただ止まる。
しかし、私はpython3 test.py
を実行した場合、filename引数なしで、その後、私はこのログを取得する:私は、タスクを停止するまで
First
In Daemon
Main Loop
Namespace(filename=None)
In If
[]
[]
After Ifs
<sqlalchemy.engine.base.Connection object at 0x7fe11c1da5f8>
は、次にそれが無限に繰り返されます。
なぜそれが止まっているのか分かりません。誰かが私を助けることができれば、それは素晴らしいだろう。 python-daemon
を使って試してみる前に、このスクリプトの別の形式がありました。だから私は基本的なロジックが健全でなければならないことを知っている
'open(args.filename) 'の例外はどのように処理されますか? – VPfB