子进程中的父记录器

时间:2018-03-20 20:41:27

标签: python logging multiprocessing posix child-process

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

# 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().

在子进程中拥有父记录器的问题是什么?

1 个答案:

答案 0 :(得分:1)

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