dougong5817 2017-04-05 16:12
浏览 75
已采纳

当缓冲区接收数据时,将exec.Command输出复制到文件

I have a script that dumps quite a bit of text into STDOUT when run. I'm trying to execute this script and write the output to a file without holding the entire buffer in memory at one time. (We're talking many megabytes of text that this script outputs at one time.)

The following works, but because I'm doing this across several goroutines, my memory consumption shoots up to > 5GB which I would really like to avoid:

var out bytes.Buffer
cmd := exec.Command("/path/to/script/binary", "arg1", "arg2")
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
    log.Fatal(err)
}
out.WriteTo(io) // io is the writer connected to the new file

Ideally as out fills up, I want to be emptying it into my target file to keep memory usage low. I've tried changing this to:

cmd := exec.Command("/path/to/script/binary", "arg1", "arg2")
cmd.Start()
stdout, _ := cmd.StdoutPipe()
r := *bufio.NewReader(stdout)
r.WriteTo(io)
cmd.Wait()

However when I print out these variables stdout is <nil>, r is {[0 0 0 0 0...]}, and r.WriteTo panics: invalid memory address or nil pointer dereference.

Is it possible to write the output of cmd as it is generated to keep memory usage down? Thanks!

  • 写回答

1条回答 默认 最新

  • duanmengmiezen8855 2017-04-05 16:34
    关注

    Why don't you just write to a file directly?

    file, _ := os.Create("/some/file")
    cmd.Stdout = file
    

    Or use your io thing (that's a terrible name for a variable, by the way, since it's a) the name of a standard library package, b) ambiguous--what does it mean?)

    cmd.Stdout = io
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部