• <tfoot id='jHiYL'></tfoot>
      • <bdo id='jHiYL'></bdo><ul id='jHiYL'></ul>

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

        <legend id='jHiYL'><style id='jHiYL'><dir id='jHiYL'><q id='jHiYL'></q></dir></style></legend>
      1. <small id='jHiYL'></small><noframes id='jHiYL'>

        新手 python 子进程:“写入错误:管道损坏"

        newbie python subprocess: quot;write error: Broken pipequot;(新手 python 子进程:“写入错误:管道损坏)
          • <bdo id='TuOa4'></bdo><ul id='TuOa4'></ul>
            <i id='TuOa4'><tr id='TuOa4'><dt id='TuOa4'><q id='TuOa4'><span id='TuOa4'><b id='TuOa4'><form id='TuOa4'><ins id='TuOa4'></ins><ul id='TuOa4'></ul><sub id='TuOa4'></sub></form><legend id='TuOa4'></legend><bdo id='TuOa4'><pre id='TuOa4'><center id='TuOa4'></center></pre></bdo></b><th id='TuOa4'></th></span></q></dt></tr></i><div id='TuOa4'><tfoot id='TuOa4'></tfoot><dl id='TuOa4'><fieldset id='TuOa4'></fieldset></dl></div>

            <legend id='TuOa4'><style id='TuOa4'><dir id='TuOa4'><q id='TuOa4'></q></dir></style></legend>
            <tfoot id='TuOa4'></tfoot>

          • <small id='TuOa4'></small><noframes id='TuOa4'>

              <tbody id='TuOa4'></tbody>

                • 本文介绍了新手 python 子进程:“写入错误:管道损坏"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  感谢以下有用的建议:

                  所以当我修复它时似乎是固定的

                  So it seems to be fixed when I

                  1. 将命令分离到对 Popen 的单独调用中
                  2. stderr=subprocess.PIPE 作为每个 Popen 链的参数.

                  新代码:

                  import subprocess
                  import shlex
                  import logging
                  
                  def run_shell_commands(cmds):
                      """ Run commands and return output from last call to subprocess.Popen.
                          For usage see the test below.
                      """
                      # split the commands
                      cmds = cmds.split("|")
                      cmds = list(map(shlex.split,cmds))
                  
                      logging.info('%s' % (cmds,))
                  
                      # run the commands
                      stdout_old = None
                      stderr_old = None
                      p = []
                      for cmd in cmds:
                          logging.info('%s' % (cmd,))
                          p.append(subprocess.Popen(cmd,stdin=stdout_old,stdout=subprocess.PIPE,stderr=subprocess.PIPE))
                          stdout_old = p[-1].stdout
                          stderr_old = p[-1].stderr
                      return p[-1]
                  
                  
                  pattern = '"^85567      "'
                  file = "j"
                  
                  cmd1 = 'grep %s %s | sort -g -k3 | head -10 | cut -d" " -f2,3' % (pattern, file)
                  p = run_shell_commands(cmd1)
                  out = p.communicate()
                  print(out)
                  

                  原帖:

                  我花了太长时间试图解决一个简单的 subprocess.Popen 管道问题.

                  I've spent too long trying to solve a problem piping a simple subprocess.Popen.

                  代码:

                  import subprocess
                  cmd = 'cat file | sort -g -k3 | head -20 | cut -f2,3' % (pattern,file)
                  p = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
                  for line in p.stdout:
                      print(line.decode().strip())
                  

                  文件的输出长度约为 1000 行:

                  Output for file ~1000 lines in length:

                  ...
                  sort: write failed: standard output: Broken pipe
                  sort: write error
                  

                  长度>241 行的文件的输出:

                  Output for file >241 lines in length:

                  ...
                  sort: fflush failed: standard output: Broken pipe
                  sort: write error
                  

                  <241 行长度的文件的输出很好.

                  Output for file <241 lines in length is fine.

                  我一直在疯狂地阅读文档和谷歌搜索,但是我缺少关于子进程模块的一些基本内容......可能与缓冲区有关.我试过 p.stdout.flush() 并使用缓冲区大小和 p.wait().我试图用像'sleep 20;'这样的命令来重现它.catmoderatefile' 但这似乎运行没有错误.

                  I have been reading the docs and googling like mad but there is something fundamental about the subprocess module that I'm missing ... maybe to do with buffers. I've tried p.stdout.flush() and playing with the buffer size and p.wait(). I've tried to reproduce this with commands like 'sleep 20; cat moderatefile' but this seems to run without error.

                  推荐答案

                  来自 子流程 文档:

                  # To replace shell pipeline like output=`dmesg | grep hda`
                  p1 = Popen(["dmesg"], stdout=PIPE)
                  p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
                  output = p2.communicate()[0]
                  

                  这篇关于新手 python 子进程:“写入错误:管道损坏"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)

                          <tbody id='1dhaE'></tbody>
                        <legend id='1dhaE'><style id='1dhaE'><dir id='1dhaE'><q id='1dhaE'></q></dir></style></legend>
                        • <bdo id='1dhaE'></bdo><ul id='1dhaE'></ul>
                          • <small id='1dhaE'></small><noframes id='1dhaE'>

                            <tfoot id='1dhaE'></tfoot>

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