将大量数据写入`exec.Command().StdinPipe()`时出现错误。

huangapple go评论79阅读模式
英文:

writing large amounts of data to exec.Command().StdinPipe() errors

问题

考虑以下Go代码片段:

cmd := exec.Command(program, arg0)
stdin, err := cmd.StdinPipe()
// 当b太大时会产生错误
n, err := stdin.Write(b.Bytes())

每当b太大时,Write()函数会返回一个错误。通过尝试不同大小的b,似乎只要b的长度超过Linux管道缓冲区的大小,就会出现这个错误。有没有办法解决这个问题?实际上,我需要通过stdin将大型日志文件传递给外部脚本。

英文:

Consider the following Go code fragment:

cmd := exec.Command(program, arg0)
stdin, err := cmd.StdinPipe()
// produces error when b is too large
n, err := stdin.Write(b.Bytes())

Whenever b is too large, Write() returns an error. Having experimented with different size bs, it would seem this occurs whenever the length of b is longer than the Linux pipe buffer size. Is there a way around this? Essentially I need to feed large log files via stdin to an external script.

答案1

得分: 1

我写了这个程序来测试你的代码:

package main

import "os/exec"
import "fmt"


func main() {
        cmd := exec.Command("/bin/cat")

        in, _ := cmd.StdinPipe()

        cmd.Start()

        for i := 1024*1024; ; i += 1024*1024 {
                b := make([]byte,i)
                n, err := in.Write(b)
                fmt.Printf("%d: %v\n", n, err)
                if err != nil {
                        cmd.Process.Kill()
                        return
                }
        }
}

只有当被调用的进程关闭标准输入时,这个程序才会报错。你调用的程序是否关闭了标准输入?这可能是 Go 运行时的一个 bug。

英文:

I wrote this program to test your code:

package main

import "os/exec"
import "fmt"


func main() {
        cmd := exec.Command("/bin/cat")

        in, _ := cmd.StdinPipe()

        cmd.Start()

        for i := 1024*1024; ; i += 1024*1024 {
                b := make([]byte,i)
                n, err := in.Write(b)
                fmt.Printf("%d: %v\n", n, err)
                if err != nil {
                        cmd.Process.Kill()
                        return
                }
        }
}

The only way this program gives an error is if the called process closes stdin. Does the program you call close stdin? This might be a bug in the Go runtime.

huangapple
  • 本文由 发表于 2014年1月8日 18:39:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/20993193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定