Merging a Python script's subprocess' stdout and stderr while keeping them distinguishable

前端 未结 6 1778
轮回少年
轮回少年 2020-12-04 14:44

I would like to direct a python script\'s subprocess\' stdout and stdin into the same file. What I don\'t know is how to make the lines from the two sources distinguishable?

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-04 15:05

    If you want to interleave to get roughly the same order that you would if you ran the process interactively then you need to do what the shell does and poll stdin/stdout and write in the order that they poll.

    Here's some code that does something along the lines of what you want - in this case sending the stdout/stderr to a logger info/error streams.

    tsk = subprocess.Popen(args,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    
    poll = select.poll()
    poll.register(tsk.stdout,select.POLLIN | select.POLLHUP)
    poll.register(tsk.stderr,select.POLLIN | select.POLLHUP)
    pollc = 2
    
    events = poll.poll()
    while pollc > 0 and len(events) > 0:
      for event in events:
        (rfd,event) = event
        if event & select.POLLIN:
          if rfd == tsk.stdout.fileno():
            line = tsk.stdout.readline()
            if len(line) > 0:
              logger.info(line[:-1])
          if rfd == tsk.stderr.fileno():
            line = tsk.stderr.readline()
            if len(line) > 0:
              logger.error(line[:-1])
        if event & select.POLLHUP:
          poll.unregister(rfd)
          pollc = pollc - 1
        if pollc > 0: events = poll.poll()
    tsk.wait()
    

提交回复
热议问题