日志记录与Python pid包冲突



我一直在使用pid开发python守护进程,在直接将初始日志记录到控制台后,我想使用python的logging模块切换到文件日志记录。这就是我遇到问题的时候。

我有start/stop功能来管理守护进程:

import os
import sys
import time
import signal
import lockfile
import logging
import logging.config
import daemon
from pid import PidFile
from mpmonitor.monitor import MempoolMonitor
# logging.config.fileConfig(fname="logging.conf", disable_existing_loggers=False)
# log = logging.getLogger("mpmonitor")

def start():
print("Starting Mempool Monitor")
_pid_file = PidFile(pidname="mpmonitor.pid", piddir=curr_dir)
with daemon.DaemonContext(stdout=sys.stdout,
stderr=sys.stderr,
stdin=sys.stdin,
pidfile=_pid_file):
# Start the monitor:
mpmonitor = MempoolMonitor()
mpmonitor.run()

def stop():
print("n{}n".format(pid_file))
try:
with open(pid_file, "r") as f:
content = f.read()
f.close()
except FileNotFoundError as fnf_err:
print("WARNING - PID file not found, cannot stop daemon.n({})".format(pid_file))
sys.exit()

print("Stopping Mempool Monitor")
# log.info("Stopping Mempool Monitor")
pid = int(content)
os.kill(pid, signal.SIGTERM)
sys.exit()

正如你所期望的那样工作。(注意日志代码有注释。(

 

取消对日志记录代码的注释会破坏一切,并且会发生一些非常随机的事情。错误消息(精简,完全回溯"看起来像垃圾邮件"(:

--- Logging error ---
OSError: [Errno 9] Bad file descriptor
Call stack:
File "/home/leilerg/.local/lib/python3.6/site-packages/pid/__init__.py", line 77, in setup
self.logger.debug("%r entering setup", self)
Message: '%r entering setup'
Arguments: (<pid.PidFile object at 0x7fc8faa479e8>,)
--- Logging error ---
OSError: [Errno 9] Bad file descriptor
Call stack:
File "/home/leilerg/.local/lib/python3.6/site-packages/pid/__init__.py", line 170, in create
self.logger.debug("%r create pidfile: %s", self, self.filename)
Message: '%r create pidfile: %s'
Arguments: (<pid.PidFile object at 0x7fc8faa479e8>, '/home/leilerg/python/mempoolmon/mpmonitor.pid')
Traceback (most recent call last):
File "/home/leilerg/.local/lib/python3.6/site-packages/pid/__init__.py", line 136, in inner_check
pid = int(pid_str)
ValueError: invalid literal for int() with base 10: 'DEBUG - 2020-04-'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/leilerg/.local/lib/python3.6/site-packages/pid/__init__.py", line 139, in inner_check
raise PidFileUnreadableError(exc)
pid.PidFileUnreadableError: invalid literal for int() with base 10: 'DEBUG - 2020-04-'
--- Logging error ---
Traceback (most recent call last):
OSError: [Errno 9] Bad file descriptor
Call stack:
File "/home/leilerg/.local/lib/python3.6/site-packages/pid/__init__.py", line 197, in close
self.logger.debug("%r closing pidfile: %s", self, self.filename)
Message: '%r closing pidfile: %s'
Arguments: (<pid.PidFile object at 0x7fc8faa479e8>, '/home/leilerg/python/mempoolmon/mpmonitor.pid')

 

我所指的随机内容现在是文件mpmonitor.pid不再包含PID,而是一些尝试的日志/错误消息

user@mylaptor:mempoolmon: cat mpmonitor.pid
DEBUG - 2020-04-05 10:52:55,676 - PidFile: <pid.PidFile object at 0x7fc8faa479e8> entering setup
DEBUG - 2020-04-05 10:52:55,678 - PidFile: <pid.PidFile object at 0x7fc8faa479e8> create pidfile: /home/leilerg/python/mempoolmon/mpmonitor.pid
DEBUG - 2020-04-05 10:52:55,678 - PidFile: <pid.PidFile object at 0x7fc8faa479e8> check pidfile: /home/leilerg/python/mempoolmon/mpmonitor.pid
DEBUG - 2020-04-05 10:52:55,678 - PidFile: <pid.PidFile object at 0x7fc8faa479e8> closing pidfile: /home/leilerg/python/mempoolmon/mpmonitor.pid

 

对我来说,这看起来像是pid日志文件不知何故与PID文件混淆了。这很奇怪,因为我明确地设置了disable_existing_loggers=False

有什么想法吗?

如果相关的话,我在最新的Linux Mint上。我还在pid项目GitHub上发布了这个问题,因为我怀疑这是一个bug。

问题已在GitHub页面第31期得到解决。

相关内容

  • 没有找到相关文章

最新更新