• <tfoot id='iTjZB'></tfoot>

      1. <i id='iTjZB'><tr id='iTjZB'><dt id='iTjZB'><q id='iTjZB'><span id='iTjZB'><b id='iTjZB'><form id='iTjZB'><ins id='iTjZB'></ins><ul id='iTjZB'></ul><sub id='iTjZB'></sub></form><legend id='iTjZB'></legend><bdo id='iTjZB'><pre id='iTjZB'><center id='iTjZB'></center></pre></bdo></b><th id='iTjZB'></th></span></q></dt></tr></i><div id='iTjZB'><tfoot id='iTjZB'></tfoot><dl id='iTjZB'><fieldset id='iTjZB'></fieldset></dl></div>

        <small id='iTjZB'></small><noframes id='iTjZB'>

        • <bdo id='iTjZB'></bdo><ul id='iTjZB'></ul>
        <legend id='iTjZB'><style id='iTjZB'><dir id='iTjZB'><q id='iTjZB'></q></dir></style></legend>

        Python子进程将孩子的输出输出到文件和终端?

        时间:2023-07-22
        <legend id='23OtM'><style id='23OtM'><dir id='23OtM'><q id='23OtM'></q></dir></style></legend>
        1. <tfoot id='23OtM'></tfoot>

            <i id='23OtM'><tr id='23OtM'><dt id='23OtM'><q id='23OtM'><span id='23OtM'><b id='23OtM'><form id='23OtM'><ins id='23OtM'></ins><ul id='23OtM'></ul><sub id='23OtM'></sub></form><legend id='23OtM'></legend><bdo id='23OtM'><pre id='23OtM'><center id='23OtM'></center></pre></bdo></b><th id='23OtM'></th></span></q></dt></tr></i><div id='23OtM'><tfoot id='23OtM'></tfoot><dl id='23OtM'><fieldset id='23OtM'></fieldset></dl></div>

                <tbody id='23OtM'></tbody>
                <bdo id='23OtM'></bdo><ul id='23OtM'></ul>

                • <small id='23OtM'></small><noframes id='23OtM'>

                  本文介绍了Python子进程将孩子的输出输出到文件和终端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I'm running a script that executes a number of executables by using

                  subprocess.call(cmdArgs,stdout=outf, stderr=errf)
                  

                  when outf/errf is either None or a file descriptor (different files for stdout/stderr).

                  Is there any way I can execute each exe so that the stdout and stderr will be written to the files and terminal together?

                  解决方案

                  The call() function is just Popen(*args, **kwargs).wait(). You could call Popen directly and use stdout=PIPE argument to read from p.stdout:

                  #!/usr/bin/env python
                  import sys
                  from subprocess import Popen, PIPE
                  from threading import Thread
                  
                  
                  def tee(infile, *files):
                      """Print `infile` to `files` in a separate thread."""
                  
                      def fanout(infile, *files):
                          with infile:
                              for line in iter(infile.readline, b""):
                                  for f in files:
                                      f.write(line)
                  
                      t = Thread(target=fanout, args=(infile,) + files)
                      t.daemon = True
                      t.start()
                      return t
                  
                  
                  def teed_call(cmd_args, **kwargs):
                      stdout, stderr = [kwargs.pop(s, None) for s in ["stdout", "stderr"]]
                      p = Popen(
                          cmd_args,
                          stdout=PIPE if stdout is not None else None,
                          stderr=PIPE if stderr is not None else None,
                          **kwargs
                      )
                      threads = []
                      if stdout is not None:
                          threads.append(
                              tee(p.stdout, stdout, getattr(sys.stdout, "buffer", sys.stdout))
                          )
                      if stderr is not None:
                          threads.append(
                              tee(p.stderr, stderr, getattr(sys.stderr, "buffer", sys.stderr))
                          )
                      for t in threads:
                          t.join()  # wait for IO completion
                      return p.wait()
                  
                  
                  outf, errf = open("out.txt", "wb"), open("err.txt", "wb")
                  assert not teed_call(["cat", __file__], stdout=None, stderr=errf)
                  assert not teed_call(["echo", "abc"], stdout=outf, stderr=errf, bufsize=0)
                  assert teed_call(["gcc", "a b"], close_fds=True, stdout=outf, stderr=errf)
                  

                  这篇关于Python子进程将孩子的输出输出到文件和终端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python C 程序子进程在“for line in iter"处挂起 下一篇:修改环境的 Python 子进程/Popen

                  相关文章

                • <tfoot id='yhHJS'></tfoot>

                  <small id='yhHJS'></small><noframes id='yhHJS'>

                  • <bdo id='yhHJS'></bdo><ul id='yhHJS'></ul>

                  <i id='yhHJS'><tr id='yhHJS'><dt id='yhHJS'><q id='yhHJS'><span id='yhHJS'><b id='yhHJS'><form id='yhHJS'><ins id='yhHJS'></ins><ul id='yhHJS'></ul><sub id='yhHJS'></sub></form><legend id='yhHJS'></legend><bdo id='yhHJS'><pre id='yhHJS'><center id='yhHJS'></center></pre></bdo></b><th id='yhHJS'></th></span></q></dt></tr></i><div id='yhHJS'><tfoot id='yhHJS'></tfoot><dl id='yhHJS'><fieldset id='yhHJS'></fieldset></dl></div>

                      <legend id='yhHJS'><style id='yhHJS'><dir id='yhHJS'><q id='yhHJS'></q></dir></style></legend>