python日志模块 超量处理_Python日志模块在多处理上下文中的混乱行为

所以我尝试在multiprocessing上下文中使用logging。假设我想让每个进程编写自己的日志文件。以下是我的设置:# worker.py

import logging

import os

def worker( x ) :

"""

Write the value of x in the log file

"""

logger = logging.getLogger( __name__ )

pid = os.getpid() # get the process id

handler = logging.FileHandler( str(pid) + ".log" )

logger.addHandler( handler )

logger.info( "pid={pid}, x={x}".format( **locals() ) )

这是我的main:

^{pr2}$

现在输出日志文件。这是main.log:pid=1985, x=0

pid=1985, x=2

pid=1986, x=1

pid=1985, x=3

pid=1986, x=4

我认为这个输出是正常的:两个子进程将事件传播到根父进程。所以它是否与Logging to a single file from multiple processes场景相对应(是吗?)。但现在是第一个子进程日志文件:pid=1985, x=0

pid=1985, x=2

pid=1985, x=2

pid=1985, x=3

pid=1985, x=3

pid=1985, x=3

这是第二个子日志文件:pid=1986, x=1

pid=1986, x=4

pid=1986, x=4

看起来每个子进程为第一个输入写入一次日志,第二次为两次,第三次为三次,等等,发生了什么?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值