23. python父进程如何将子进程的日志直接输出到文件

子进程代码 child.py

import sys

if __name__ == '__main__':
    print("stdout 输出", file=sys.stdout)  # 模拟info日志
    print("stderr 输出", file=sys.stderr)  # 模拟错误日志

父进程代码Father.py

import time

import os, traceback

import subprocess


def open_process(script_path, stdout_file, stderr_file):
    args = ['python', script_path]
    process = subprocess.Popen(
        # stdin 表示可以接收输入
        args,
        stdin=subprocess.PIPE,
        stdout=stdout_file,
        stderr=stderr_file,
        env=os.environ.copy(),
    )
    i = 1
    while True:
        code = process.poll()
        if code is None:
            time.sleep(0.1)
            print("[Starter] script_path=%s still running" % script_path)
            i = i + 1
            continue
        elif code == 0:
            # out,stderr = process.communicate() 此时stdout和error都没有数据,因为都被重定向到文件中了
            print("[Starter] script_path=%s success" % script_path)
            break
        else:
            raise Exception("子进程异常")


def create_dir(path):
    directory = os.path.dirname(path)

    # 如果目录不存在,则创建它
    if not os.path.exists(directory):
        os.makedirs(directory)


if __name__ == "__main__":
    # 构建子进程的日志文件
    stdout_path = os.path.join("./", "logs", "stdout")
    stderr_path = os.path.join("./", "logs", "stderr")
    # 目录不存在则创建
    create_dir(stdout_path)
    create_dir(stderr_path)
    stdout = open(stdout_path, "a")
    stderr = open(stderr_path, "a")
    try:
        open_process("child.py", stdout, stderr)
    except Exception as e:
        traceback.print_exc()
    finally:
        stdout.close()  # 关闭文件
        stderr.close()  # 关闭文件

应用场景

subprocess.Popen() 可以执行shell命令,可以执行python文件,可以执行java jar包。 因此在进程大型框架研发的时候会经常用到, 这样的话其实子进程完全可以避免将日志打印在文件中,直接输出到控制台即可。 日志可用上面的方式由父进程进行收集。

  • 13
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我先森

鼓励一个吧,哈哈

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值