douqingnao9246 2016-08-10 07:33
浏览 70
已采纳

流命令从Goroutine输出进度

Streaming commands output progress question addresses the problem of printing progress of a long running command.

I tried to put the printing code within a goroutine but the scanner claims to have already hit the EOF immediately and the for block is never executed.

The bufio.scan code that gets executed on the first execution of the Scan() method is:

    // We cannot generate a token with what we are holding.
    // If we've already hit EOF or an I/O error, we are done.
    if s.err != nil {
        // Shut it down.
        s.start = 0
        s.end = 0
        return false
    }

And if I print s.err the output is EOF.

The code I'm trying to run is:

cmd := exec.Command("some", "command")
c := make(chan int, 1)

go func(cmd *exec.Cmd, c chan int) {
    stdout, _ := cmd.StdoutPipe()

    <-c

    scanner := bufio.NewScanner(stdout)
    for scanner.Scan() {
        m := scanner.Text()
        fmt.Println(m)
    }
}(cmd, c)

cmd.Start()

c <- 1

cmd.Wait()

The idea is to start the Goroutine, get a hold of the cmd.stdout, wait that the cmd is started, and start processing its output.

The result is that the long command gets executed and the program waits for its completion, but nothing is printed to terminal.

Any idea why by the time scanner.Scan() is invoked for the first time the stdout has already reached EOF?

  • 写回答

2条回答 默认 最新

  • douhuxi4145 2016-08-10 10:20
    关注

    There are some problems:

    • The pipe is being closed before reading all data.
    • Always check for errors
    • Start cmd.Start() after c <- struct{}{} and use unbuffered channel c := make(chan struct{})

    Two working sample codes:

    1: Wait using channel then close the pipe after EOF using defer func() { c <- struct{}{} }(), like this working sample code:

    package main
    
    import (
        "bufio"
        "fmt"
        "os/exec"
    )
    
    func main() {
        cmd := exec.Command("Streamer")
        c := make(chan struct{})
    
        go run(cmd, c)
    
        c <- struct{}{}
        cmd.Start()
    
        <-c
        if err := cmd.Wait(); err != nil {
            fmt.Println(err)
        }
        fmt.Println("done.")
    }
    
    func run(cmd *exec.Cmd, c chan struct{}) {
        defer func() { c <- struct{}{} }()
        stdout, err := cmd.StdoutPipe()
        if err != nil {
            panic(err)
        }
        <-c
        scanner := bufio.NewScanner(stdout)
        for scanner.Scan() {
            m := scanner.Text()
            fmt.Println(m)
        }
        fmt.Println("EOF")
    }
    

    2: Also you may Wait using sync.WaitGroup, like this working sample code:

    package main
    
    import (
        "bufio"
        "fmt"
        "os/exec"
        "sync"
    )
    
    var wg sync.WaitGroup
    
    func main() {
        cmd := exec.Command("Streamer")
        c := make(chan struct{})
        wg.Add(1)
        go func(cmd *exec.Cmd, c chan struct{}) {
            defer wg.Done()
            stdout, err := cmd.StdoutPipe()
            if err != nil {
                panic(err)
            }
            <-c
            scanner := bufio.NewScanner(stdout)
            for scanner.Scan() {
                m := scanner.Text()
                fmt.Println(m)
            }
        }(cmd, c)
    
        c <- struct{}{}
        cmd.Start()
    
        wg.Wait()
        fmt.Println("done.")
    }
    

    And Streamer sample code (just for testing):

    package main
    
    import "fmt"
    import "time"
    
    func main() {
        for i := 0; i < 10; i++ {
            time.Sleep(1 * time.Second)
            fmt.Println(i, ":", time.Now().UTC())
        }
    }
    

    And see func (c *Cmd) StdoutPipe() (io.ReadCloser, error) Docs:

    StdoutPipe returns a pipe that will be connected to the command's standard output when the command starts.

    Wait will close the pipe after seeing the command exit, so most callers need not close the pipe themselves; however, an implication is that it is incorrect to call Wait before all reads from the pipe have completed. For the same reason, it is incorrect to call Run when using StdoutPipe. See the example for idiomatic usage.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图