英文:
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 b
s, 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论