新手python子进程:“写入错误:管道损坏"; [英] newbie python subprocess: "write error: Broken pipe"

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

问题描述

感谢以下有用的建议:

所以我看来这是固定的

  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)

原始帖子:

我花了很长时间尝试解决通过简单的子流程管道打开的问题.

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;猫中等文件",但这似乎没有错误.

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子进程:“写入错误:管道损坏";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆