多处理中的 Python 日志记录

标签 python logging multiprocessing

如何将日志输出到多个进程的控制台? 示例:

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库,但没有帮助

最佳答案

您的(更新的)代码在这里运行良好:

INFO:root:This is logging for TEST1
INFO:root:This is logging for TEST1
INFO:root:This is logging for TEST1
INFO:root:This is logging for TEST1
INFO:root:This is logging for TEST1
INFO:root:This is logging for TEST1
INFO:root:This is logging for TEST1
INFO:root:This is logging for TEST1
INFO:root:This is logging for TEST1
INFO:root:This is logging for TEST2
INFO:root:This is logging for TEST1
INFO:root:This is logging for TEST1
INFO:root:This is logging for TEST2
INFO:root:This is logging for TEST2
INFO:root:This is logging for TEST2
INFO:root:This is logging for TEST2
INFO:root:This is logging for TEST2
INFO:root:This is logging for TEST2
INFO:root:This is logging for TEST2
INFO:root:This is logging for TEST2
INFO:root:This is logging for TEST2
INFO:root:This is logging for TEST2
INFO:root:This is logging for TEST2
INFO:root:This is logging for TEST2
INFO:root:This is logging for TEST2
INFO:root:This is logging for TEST2

如果它不适合你那么你应该 file a bug report with multiprocessing-logging .

关于多处理中的 Python 日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40883481/

相关文章:

Grails log4j appender 配置似乎被忽略了

ios - 使用新的 swift os_log api 读取日志

python - Gevent 猴子补丁破坏多处理

python - python 中的多重处理 - 通过添加结果来节省内存

iphone - 用于二进制 plist 的 Python 模块

python - 如何在不重新启动Python的情况下清空缓冲区

java - 日志记录不适用于 Spring boot 2.1.1 和 JDK 11 升级

python - 如何从 Python 脚本中列出 Python 可用的所有包/模块?

python - 12296 :26672:0420/163936. 459 :ERROR:browser_switcher_service. cc(238) XXX Init() "Selenium Python"错误

mpi 中的 Python 多处理