python logging with multiprocessing, root logger different in windows

后端 未结 1 1310
我寻月下人不归
我寻月下人不归 2021-01-18 18:47

I tried logging with multiprocessing, and found under windows, I will get different root logger in child process, but under Linux that is ok.

The test code:

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-18 18:56

    It seems to me that this could be linked to the following platform-dependant behaviour:

    16.6.3.2. Windows Since Windows lacks os.fork() it has a few extra restrictions:

    (...)

    Global variables

    Bear in mind that if code run in a child process tries to access a global variable, then the value it sees (if any) may not be the same as the value in the parent process at the time that Process.start was called.

    However, global variables which are just module level constants cause no problems.

    From your question, I assume that this results in a logging.basicConfig() call that is not reaching all your processes. A solution to this is to have your child processes to log to a Queue (using QueueHandler), and have a dedicated thread in your main process that will listen to the queue.

    0 讨论(0)
提交回复
热议问题