【问题标题】:Parent loggers in child processes子进程中的父记录器
【发布时间】:2018-03-20 20:41:27
【问题描述】:

在 Python 的 Logging Cookbook 中,"A more elaborate multiprocessing example" 部分中有以下 cmets:

# The worker process configuration is just a QueueHandler attached to the
# root logger, which allows all messages to be sent to the queue.
# We disable existing loggers to disable the "setup" logger used in the
# parent process. This is needed on POSIX because the logger will
# be there in the child following a fork().

# The listener process configuration shows that the full flexibility of
# logging configuration is available to dispatch events to handlers however
# you want.
# We disable existing loggers to disable the "setup" logger used in the
# parent process. This is needed on POSIX because the logger will
# be there in the child following a fork().

在子进程中有父记录器到底有什么问题?

【问题讨论】:

  • 回复您的评论 - 食谱只是展示您可以如何做事的指南 - 您可以随时根据您的具体需求调整代码。

标签: python logging multiprocessing posix child-process


【解决方案1】:

(潜在的)问题是,如果父进程和子进程一样继续记录,它们可能会记录到相同的处理程序(因为 fork 在 POSIX 上的工作方式)并且您不能保证同时从两个进程写入单个文件将正常工作。请参阅食谱中this section 的第一段。

【讨论】:

  • 顺便说一句,没有比在多处理部分中配置logging 模块within 函数(可能被多次调用)更好的方法吗?例如,在测试这些函数时,它们只记录第一次调用,但不记录后续调用,因为disable_existing_loggers=True。如果我设置disable_existing_loggers=False,我会从第三方模块获取我不想要的日志。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-16
  • 2014-07-14
  • 2019-04-17
  • 2019-02-21
相关资源
最近更新 更多