dongpo2002 2016-04-15 13:56
浏览 30
已采纳

OS / exec优雅,循环兼容的stdin和stdout输入/输出

Example script is just wrapper to "wc -m" command, simple symbol counter. I trying just feed input with "teststrings" slice elements. And receive number of symbol of each string at output listener goroutine. Looking for a way to make "wc" listen forever for input at all. I'v notice when i increase sleep to

time.Sleep(6000 * time.Nanosecond)

wc don't wait for input.

package main

import (
    "bytes"
    "fmt"
    "os/exec"
    "time"
)

func main() {
    BashCommand := exec.Command("wc", "-m")
    InputBytes := &bytes.Buffer{}
    OutputBytes := &bytes.Buffer{}
    BashCommand.Stdin = InputBytes
    BashCommand.Stdout = OutputBytes
    e := BashCommand.Start()
    time.Sleep(1 * time.Nanosecond)
    _, _ = InputBytes.Write([]byte("13symbolsting"))
    if e != nil {
        fmt.Println(e)
    }
    fmt.Println("after run")

    teststrings := []string{
        "one",
        "twoo",
        "threeeee",
    }
    for _, s := range teststrings {
        _, _ = InputBytes.Write([]byte(s))

    }

    //result printer
    go func() {
        for {
            line, _ := OutputBytes.ReadString('
')
            if line != "" {
                fmt.Println(line)
            }
        }
    }()
    var input string
    fmt.Scanln(&input) //dont exit until keypress

}
  • 写回答

1条回答 默认 最新

  • douhu7807 2016-04-15 14:21
    关注

    If you increase the sleep to a large value, the goroutine started by the command to pump InputBytes to the process runs before data is written to InputBytes. The goroutine closes the pipe to the child and exits without having read any data.

    Use pipes instead of bytes.Buffer:

    c := exec.Command("wc", "-m")
    w, _ := c.StdinPipe()
    r, _ := c.StdoutPipe()
    if err := c.Start(); err != nil {
        log.Fatal(err)
    }
    
    w.Write([]byte("13symbolsting"))
    teststrings := []string{
        "one",
        "twoo",
        "threeeee",
    }
    for _, s := range teststrings {
        w.Write([]byte(s))
    
    }
    w.Close() // Close pipe to indicate input is done.
    
    var wg sync.WaitGroup
    wg.Add(1)
    
    go func() {
        s := bufio.NewScanner(r)
        for s.Scan() {
            fmt.Println(s.Text())
        }
        wg.Done()
    }()
    
    wg.Wait()
    

    Another option is to write to the bytes.Buffer before starting the command and wait for command to complete before reading the output:

    c := exec.Command("wc", "-m")
    var w, r bytes.Buffer
    c.Stdin = &w
    c.Stdout = &r
    
    // Write data before starting command.
    
    w.Write([]byte("13symbolsting"))
    teststrings := []string{
        "one",
        "twoo",
        "threeeee",
    }
    for _, s := range teststrings {
        w.Write([]byte(s))
    
    }
    
    if err := c.Start(); err != nil {
        log.Fatal(err)
    }
    
    // Wait for command to complete before reading data.
    
    if err := c.Wait(); err != nil {
        log.Fatal(err)
    }
    
    s := bufio.NewScanner(&r)
    for s.Scan() {
        fmt.Println(s.Text())
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元