Python:为什么登录多处理无法正常工作 [英] Python: why logging in multiprocessing not working

查看:74
本文介绍了Python:为什么登录多处理无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将脚本从Mac(都为python 2.7.*)从Mac移植到Windows后,我发现所有日志记录都无法在子进程中使用,只有father的日志记录才写入文件.这是我的示例代码:

After I port my script to Windows from Mac (both python 2.7.*), I find that all the logging not working in subprocess, only the father's logging are write to file. Here is my example code:

# test log among multiple process env
import logging, os
from multiprocessing import Process

def child():
    logging.info('this is child')

if __name__ == '__main__':
    logging.basicConfig(filename=os.path.join(os.getcwd(), 'log.out'),
            level = logging.DEBUG, filemode='w',
            format = '[%(filename)s:%(lineno)d]: %(asctime)s - %(levelname)s: %(message)s')


    p = Process(target = child, args = ())
    p.start()
    p.join()

    logging.info('this is father')

输出仅将this is father写入log.out,并且孩子的日志丢失.如何在子进程中进行日志记录唤醒?

the output only write this is father into log.out, and the child's log missing. How to make logging woking in child process?

推荐答案

每个子进程都是一个独立的进程,父进程中的文件句柄可能会在派生后关闭(假定为POSIX).无论如何,都不支持从多个进程登录到同一文件.请参见文档了解建议的方法.

Each child is an independent process, and file handles in the parent may be closed in the child after a fork (assuming POSIX). In any case, logging to the same file from multiple processes is not supported. See the documentation for suggested approaches.

这篇关于Python:为什么登录多处理无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆