我尝试使用多进程进行日志记录,发现在Windows下,子进程中会得到不同的根记录器,但在Linux下则没有问题。
测试代码:
main.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import multiprocessing
from mymod import func
def m_func():
server = multiprocessing.Process(target=func, args=())
server.start()
logger = logging.getLogger()
#print 'in global main: ', logger
if __name__ == '__main__':
print 'in main: ', logger
m_func()
mymod.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
logger = logging.getLogger()
# print 'in global func: ', logger
def func():
print 'in func: ', logger
在Linux下,结果为:
in main: <logging.RootLogger object at 0x10e4d6d90>
in func: <logging.RootLogger object at 0x10e4d6d90>
但在 Windows 7 的 64 位系统下,主函数和功能函数之间的根记录器将会不同:
in main: <logging.RootLogger object at 0x00000000021FFD68>
in func: <logging.RootLogger object at 0x00000000023BC898>
如果我在主脚本中初始化根记录器,那么在Windows下如何在子进程中保留级别等设置?