在Windows中使用多处理进行Python日志记录

时间:2021-07-29 18:18:47

I have a rather large Python project which currently runs on Linux but I am trying to expand to Windows. I've reduced the code to a full example which can be run to illustrate my problems: I have two classes, Parent and Child. Parent is initialized first, creates a logger, and spawns a Child to do work:

我有一个相当大的Python项目,目前在Linux上运行,但我正在尝试扩展到Windows。我已经将代码缩减为完整的示例,可以运行以说明我的问题:我有两个类,父和子。首先初始化Parent,创建一个记录器,并生成一个Child来完成工作:

import logging
import logging.config
import multiprocessing

class Parent( object ):
    def __init__(self, logconfig):
        logging.config.dictConfig(logconfig)
        self.logger = logging.getLogger(__name__)

    def spawnChild(self):
        self.logger.info('One')
        c = Child(self.logger)
        c.start()

class Child(multiprocessing.Process):
    def __init__(self, logger):
        multiprocessing.Process.__init__(self)
        self.logger = logger

    def run(self):
        self.logger.info('Two')

if __name__ == '__main__':
    p = Parent({
            'version':1, 
            "handlers": {
                "console": {
                    "class": "logging.StreamHandler",
                    "stream": "ext://sys.stdout"
                },
            },
            "root": {
                "level": "DEBUG",
                "handlers": [
                    "console",
                    ]
                }
            }
        )
    p.spawnChild()

On linux (specifically, ubuntu 12.04), I get the following (expected) output:

在linux(特别是ubuntu 12.04)上,我得到以下(预期)输出:

user@ubuntu:~$ python test.py 
One
Two

But, on Windows (specifically, Windows 7), it fails with a pickling error:

但是,在Windows(特别是Windows 7)上,它会因为酸洗错误而失败:

C:\>python test.py
<snip>
pickle.PicklingError: Can't pickle <type 'thread.lock'>: it's not found as thread.lock

The problem comes down to Windows' lack of a true fork, so objects have to be pickled when sent between threads. But, the logger can't be pickled. I've tried using __getstate__ and __setstate__ to avoid pickling, and reference by name in Child:

问题归结为Windows缺少真正的fork,因此在线程之间发送时必须对对象进行pickle。但是,记录器不能被腌制。我尝试使用__getstate__和__setstate__来避免酸洗,并在Child中按名称引用:

def __getstate__(self):
    d = self.__dict__.copy()
    if 'logger' in d.keys():
        d['logger'] = d['logger'].name
    return d

def __setstate__(self, d):
    if 'logger' in d.keys():
        d['logger'] = logging.getLogger(d['logger'])
    self.__dict__.update(d)

This works in Linux just as before, and now Windows won't fail with the PicklingError. However, my output is only from Parent:

这与以前一样在Linux中运行,现在Windows不会因PicklingError而失败。但是,我的输出仅来自Parent:

C:\>python test.py
One

C:\>

It seems that the child is unable to use the logger, despite no message complaining "No logger could be found for handler '__main__'" or any other error message. I've looked around and there are means by which I could completely restructure how I log in my program, but that's obviously a last resort. I'm hoping that I'm just missing something obvious, and that the wisdom of the crowd can point it out to me.

看起来孩子无法使用记录器,尽管没有消息抱怨“找不到处理器'__main__'的记录器”或任何其他错误消息。我环顾四周,有办法可以完全重构我登录程序的方式,但这显然是最后的手段。我希望我只是遗漏了一些明显的东西,并且人群的智慧可以向我指出。

1 个解决方案

#1


2  

In most cases, Logger objects are not picklable, because they use unpicklable theading.Lock and/or file objects internally. Your attempted workaround does avoid pickling the logger, but it ends up creating a completely different Logger in the child process, which happens to have the same name as the Logger in the parent; the effects of logging.config call you made are lost. To get the behavior you want you'll need to need to recreate the logger in the child process and re-call logging.config.dictConfig:

在大多数情况下,Logger对象不可选,因为它们在内部使用不可打击的theading.Lock和/或文件对象。您尝试过的解决方法确实避免了腌制记录器,但它最终在子进程中创建了一个完全不同的Logger,它恰好与父进程中的Logger同名;您执行的logging.config调用的效果会丢失。要获得所需的行为,您需要在子进程中重新创建记录器并重新调用logging.config.dictConfig:

class Parent( object ):
    def __init__(self, logconfig):
        self.logconfig = logconfig
        logging.config.dictConfig(logconfig)
        self.logger = logging.getLogger(__name__)

    def spawnChild(self):
        self.logger.info('One')
        c = Child(self.logconfig)
        c.start()

class Child(multiprocessing.Process):
    def __init__(self, logconfig):
        multiprocessing.Process.__init__(self)
        self.logconfig = logconfig

    def run(self):
        # Recreate the logger in the child
        logging.config.dictConfig(self.logconfig)
        self.logger = logging.getLogger(__name__)

        self.logger.info('Two')

Or, if you want to keep using __getstate__/__setstate__:

或者,如果您想继续使用__getstate __ / __ setstate__:

class Parent( object ):
    def __init__(self, logconfig):
        logging.config.dictConfig(logconfig)
        self.logger = logging.getLogger(__name__)
        self.logconfig = logconfig

    def spawnChild(self):
        self.logger.info('One')
        c = Child(self.logger, self.logconfig)
        c.start()

class Child(multiprocessing.Process):
    def __init__(self, logger, logconfig):
        multiprocessing.Process.__init__(self)
        self.logger = logger
        self.logconfig = logconfig

    def run(self):
        self.logger.info('Two')

    def __getstate__(self):
        d = self.__dict__.copy()
        if 'logger' in d:
            d['logger'] = d['logger'].name
        return d

    def __setstate__(self, d):
        if 'logger' in d:
            logging.config.dictConfig(d['logconfig'])
            d['logger'] = logging.getLogger(d['logger'])
        self.__dict__.update(d)

#1


2  

In most cases, Logger objects are not picklable, because they use unpicklable theading.Lock and/or file objects internally. Your attempted workaround does avoid pickling the logger, but it ends up creating a completely different Logger in the child process, which happens to have the same name as the Logger in the parent; the effects of logging.config call you made are lost. To get the behavior you want you'll need to need to recreate the logger in the child process and re-call logging.config.dictConfig:

在大多数情况下,Logger对象不可选,因为它们在内部使用不可打击的theading.Lock和/或文件对象。您尝试过的解决方法确实避免了腌制记录器,但它最终在子进程中创建了一个完全不同的Logger,它恰好与父进程中的Logger同名;您执行的logging.config调用的效果会丢失。要获得所需的行为,您需要在子进程中重新创建记录器并重新调用logging.config.dictConfig:

class Parent( object ):
    def __init__(self, logconfig):
        self.logconfig = logconfig
        logging.config.dictConfig(logconfig)
        self.logger = logging.getLogger(__name__)

    def spawnChild(self):
        self.logger.info('One')
        c = Child(self.logconfig)
        c.start()

class Child(multiprocessing.Process):
    def __init__(self, logconfig):
        multiprocessing.Process.__init__(self)
        self.logconfig = logconfig

    def run(self):
        # Recreate the logger in the child
        logging.config.dictConfig(self.logconfig)
        self.logger = logging.getLogger(__name__)

        self.logger.info('Two')

Or, if you want to keep using __getstate__/__setstate__:

或者,如果您想继续使用__getstate __ / __ setstate__:

class Parent( object ):
    def __init__(self, logconfig):
        logging.config.dictConfig(logconfig)
        self.logger = logging.getLogger(__name__)
        self.logconfig = logconfig

    def spawnChild(self):
        self.logger.info('One')
        c = Child(self.logger, self.logconfig)
        c.start()

class Child(multiprocessing.Process):
    def __init__(self, logger, logconfig):
        multiprocessing.Process.__init__(self)
        self.logger = logger
        self.logconfig = logconfig

    def run(self):
        self.logger.info('Two')

    def __getstate__(self):
        d = self.__dict__.copy()
        if 'logger' in d:
            d['logger'] = d['logger'].name
        return d

    def __setstate__(self, d):
        if 'logger' in d:
            logging.config.dictConfig(d['logconfig'])
            d['logger'] = logging.getLogger(d['logger'])
        self.__dict__.update(d)