编辑:Repo with all code (分支"daemon")。问题是关于链接到的文件中的代码)。
我的主程序将日志配置如下(选项已简化):
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如果我关闭了对守护进程中文件的日志记录,我的主应用程序中的日志记录就会正常工作,如果我关闭主应用程序中的文件日志记录,那么守护进程中的日志记录就会正常工作。如果我将它们设置为记录到一个文件(甚至是不同的文件),我会得到上面的错误。
发布于 2020-05-17 00:51:25
在尝试了许多方法之后,下面是有效的方法:
def process_wrapper():
with self.click_context:
self.process_queue()
def watch_wrapper():
with self.click_context:
self.watch_files()
with daemon.DaemonContext(
pidfile=daemon.pidfile.PIDLockFile(self.pid_file),
files_preserve=[logger.handlers[0].stream.fileno()],
stderr=self.log_file,
stdout=self.log_file
):
logging.info("Started cstash daemon")
while True:
threading.Thread(target=process_wrapper).start()
time.sleep(5)
threading.Thread(target=watch_wrapper).start()有两个主要的错误:
daemon.DaemonContext需要将files_preserve设置为文件日志处理程序,因此一旦切换上下文,它就不会关闭文件。这是原始problem.while True循环阻止了另一个方法的运行,因此将它们放在单独的线程中意味着它们都可以运行https://stackoverflow.com/questions/61696936
复制相似问题