python日志与多处理,根记录器在Windows中不同
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python日志与多处理,根记录器在Windows中不同,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1604字,纯文字阅读大概需要3分钟。
内容图文
我尝试使用多处理进行日志记录,并在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位下,我将在main和func之间获得不同的根记录器:
in main: <logging.RootLogger object at 0x00000000021FFD68>
in func: <logging.RootLogger object at 0x00000000023BC898>
如果我在主脚本中初始化根记录器,如何在windows下保留子进程中的级别等设置?
解决方法:
在我看来,这可能与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.
从您的问题,我认为这导致logging.basicConfig()调用没有到达您的所有进程.解决此问题的方法是让您的子进程登录到队列(使用QueueHandler),并在主进程中有一个专用线程来侦听队列.
内容总结
以上是互联网集市为您收集整理的python日志与多处理,根记录器在Windows中不同全部内容,希望文章能够帮你解决python日志与多处理,根记录器在Windows中不同所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。