pip パッケージ python-daemon を使用してタスクの 1 つをデーモン化したいのですが、考えられる最も単純な例でも実行できません。
次のコード スニペットは、「Hallo」を表示し、「Before with ステートメント」をログに記録しますが、他には何もしません。
内部に time.sleep() を入れると、デーモンの pid ファイルが作成および削除され、ジョブが長く続きますが、何もログに記録されません。「After with ..」もログに表示されません。
誰かが私がここで間違っていることを指摘できますか?
前もって感謝します。
#!/usr/bin/python3
# pip3 install python-daemon
import daemon
import lockfile
import logging
LOG_FORMAT = "%(levelname)s %(asctime)s - %(message)s"
logging.basicConfig(filename="/tmp/test.log",
level=logging.DEBUG,
format=LOG_FORMAT)
logger = logging.getLogger()
context = daemon.DaemonContext(working_directory='/tmp/',
pidfile=lockfile.FileLock('/tmp /test.pid'),
)
logger.info("Before with statement")
print("Hallo")
with context:
logger.info("Info")
logger.info("After with statement")