编辑:Repo with all code(分支“守护进程”)。问题是关于链接到的文件中的代码)
我的主程序如下配置日志记录(选项已简化):
logging.basicConfig(level='DEBUG', filename="/some/directory/cstash.log")
我的应用程序的一部分启动了一个守护进程,为此我使用了daemon
包:
with daemon.DaemonContext(
pidfile=daemon.pidfile.PIDLockFile(self.pid_file),
stderr=self.log_file,
stdout=self.log_file
):
self.watch_files()
其中self.log_file
是我打开准备编写的文件
启动应用程序时,我得到:
--- Logging error ---
Traceback (most recent call last):
File "/Users/afraz/.pyenv/versions/3.7.2/lib/python3.7/logging/__init__.py", line 1038, in emit
self.flush()
File "/Users/afraz/.pyenv/versions/3.7.2/lib/python3.7/logging/__init__.py", line 1018, in flush
self.stream.flush()
OSError: [Errno 9] Bad file descriptor
如果我关闭了对守护进程中某个文件的日志记录,则主应用程序中的日志记录工作正常;如果我关闭了对主应用程序中某个文件的日志记录,则守护进程中的日志记录工作正常。如果我将它们设置为登录到一个文件(甚至是不同的文件),就会出现上述错误
在尝试了许多方法之后,以下是有效的方法:
有两个主要问题:
daemon.DaemonContext
需要将files_preserve
设置为文件日志处理程序,以便在切换上下文后不会关闭文件。这是原始问题的实际解决方案李>while True
循环正在停止另一个方法的运行,因此将它们放在单独的线程中意味着它们都可以运行相关问题 更多 >
编程相关推荐