简体   繁体   中英

Why does logging.Logger reset its state in new process on Windows?

I'm running into strange problem with python logging module on windows. The code below shows it:

import multiprocessing
import logging
import sys



class ProcessLogger(multiprocessing.Process):
    def __init__(self):
        super().__init__()
        self.create_logger()
        print('state of logger in main proccess:')
        print(self.logger)
        print(self.logger.handlers)

    def run(self):
        print('state of logger in child proccess:')
        print(self.logger)
        print(self.logger.handlers)

    def create_logger(self):
        self.logger = logging.getLogger('something')
        self.logger.setLevel(logging.DEBUG)
        handler = logging.StreamHandler()
        self.logger.addHandler(handler)


if __name__ == '__main__':
    logg = ProcessLogger()
    logg.start()
    logg.join()
    print(sys.version)

it prints following output on Windows:

state of logger in main proccess:
<Logger something (DEBUG)>
[<StreamHandler <stderr> (NOTSET)>]
state of logger in child proccess:
<Logger something (WARNING)>
[]
3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)]

for some reason logger object in new process has default state.

I tried this on ubuntu, and it seems to work as expected:

state of logger in main proccess:
<Logger something (DEBUG)>
[<StreamHandler <stderr> (NOTSET)>]
state of logger in child proccess:
<Logger something (DEBUG)>
[<StreamHandler <stderr> (NOTSET)>]
3.7.1 (default, Oct 22 2018, 11:21:55) 
[GCC 8.2.0]

Is this expected behaviour on windows? Can someone explain this?

The reason you observe the above behaviour on the two Operating Systems is due to the difference between the fork (Unix) and the spawn (Windows) process starting strategies .

With the fork strategy, a child process is created as an exact clone of the parent process. The two processes will be identical at first and the child will share all the properties (opened files, configuration choices etc..) of the parent.

With the spawn strategy instead, a new blank process is started. The process is given the Python module to load and pointer to the first instruction to execute in the run method. All the operations performed by the parent beforehand are not inherited.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM