2017-05-23 4 views
0

私はシステムログを読むためのスクリプトを書いています。私はそれをデーモン化する際に問題に遭遇しています。それは無作為に掛けられていて、なぜ私が私の人生のために理解することはできません。私は、エラーがキャッチされている場所を把握するために行くように私はすべてのログを記録しています 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を使って試してみる前に、このスクリプトの別の形式がありました。だから私は基本的なロジックが健全でなければならないことを知っている

+0

'open(args.filename) 'の例外はどのように処理されますか? – VPfB

答えて

0

この

daemon_context = daemon.DaemonContext(files_preserve=[fh.stream]) 
with daemon_context: 

がグローバルであること、あなたのロガーあれば、私もよく分からない

with daemon.DaemonContext(files_preserve=[fh.stream]) as daemon_context: 

と同じではありません、デーモン化時のメイン処理の死を存続します。 main()の内部で初期化を試しましたか?

+0

私がコンテキストマネージャを正しく理解している場合、2つの唯一の違いは、 'with'ブロック内の 'daemon_context'変数の値です。しかし、この場合は使用されません。 – VPfB

関連する問題