【发布时间】:2016-11-30 08:26:18
【问题描述】:
如何将日志输出到多个进程的控制台? 示例:
import multiprocessing, logging, multiprocessing_logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
multiprocessing_logging.install_mp_handler(logger)
def worker():
while True:
logger.info("This is logging for TEST1")
def worker2():
while True:
logger.info("This is logging for TEST2")
if __name__ == '__main__':
p1 = multiprocessing.Process(target=worker)
p1.daemon = True
p1.start()
p2 = multiprocessing.Process(target=worker2)
p2.daemon = True
p2.start()
p1.join()
p2.join()
但是输出不正确:
INFO:root:This is logging for TEST1
INFO:root:This is logging for TEST2
IINFO:root:This is logging for TEST1
NFO:root:This is logging for TEST2
INFO:root:This is logging for TEST1
INFO:root:This is logging for TEST2
INFO:root:This is logging for TEST1
IINFO:root:This is logging for TEST2
NFO:root:This is logging for TEST1
IINFO:root:This is logging for TEST2
NFO:root:This is logging for TEST1
我尝试使用multiprocessing-logging 库,但没有帮助
【问题讨论】:
-
multiprocessing-logging看起来应该可以解决您的问题——您能否提供一个minimal reproducible example 来说明您在使用它时什么不起作用? -
我只是导入 multiprocessing-logging 并在配置日志记录后添加 multiprocessing_logging.install_mp_handler() 就像写的 link
-
在这种情况下,您应该使用file a bug report 和
multiprocessing-logging。 -
我不认为这是错误,因为在创建新进程时,新的记录器对象也会创建,但记录器对象必须是所有进程的一个。我想我误用了它
-
然后向我们展示您使用
multiprocessing-logging的代码,以便我们帮助您找到问题。
标签: python logging multiprocessing