我正在使用Golang开始一个过程并监视输出。该过程将长时间运行,我需要能够发送信号以结束该信号。

我有以下代码在大多数时间内运行良好。但是,由于某种原因,该过程可能没有输出,并且FO循环将被Scan()方法阻止,并且未能从processFinishChan接收。

是否有一种简单的方法来为Scan()方法设置超时?我尝试了一种在另一个goroutine中运行Scan()的解决方案,并使用另一个选择从新的Goroutine和一个超时通道接收,但是考虑到外部for环,是否会有越来越多的goroutines被Scan阻止吗?

// code that start the process...
scanner := bufio.NewScanner(stdout)

for {
    select {
    case <-processFinishChan: // send to this channel to terminate the process
        log.Println("Killing Process")
        err := cmdObject.Process.Kill()
        if err != nil {
            log.Printf("Error Killing: %v", err)
        } else {
            return
        }
    default:
        // default case, read the output of process and send to user.
        if !scanner.Scan() && scanner.Err() == nil {
            // reach EOF
            return
        }
        m := scanner.Bytes()

        WSOutChanHolder.mu.Lock()
        for _, ch := range WSOutChanHolder.data {
            ch <- m
        }
        WSOutChanHolder.mu.Unlock()
    }
}
分析解答

假设stdoutcmdObject.StdoutPipe()的结果,则读者应关闭读者并在过程退出后等待时中断任何正在进行的读数。

Wait will close the pipe after seeing the command exit, so most callers need not close the pipe themselves.

因此,我们需要在单独的goroutine中杀死该过程,然后在杀死该过程以观察它并关闭读者之后Wait

// code that start the process...
scanner := bufio.NewScanner(stdout)

go func() {
    <-processFinishChan: // send to this channel to terminate the process
    log.Println("Killing Process")
    err := cmdObject.Process.Kill()
    if err != nil {
        log.Printf("Error Killing: %v", err)
    } 

    cmdObject.Wait()
} ()

for {
    // default case, read the output of process and send to user.
    if !scanner.Scan() && scanner.Err() == nil {
        // reach EOF
        return
    }
    m := scanner.Bytes()

    WSOutChanHolder.mu.Lock()
    for _, ch := range WSOutChanHolder.data {
        ch <- m
    }
    WSOutChanHolder.mu.Unlock()
}