我正在使用Golang启动一个进程并监视输出.这一过程将运行很长时间,我需要能够发送信号来结束它.

我有以下代码,它在大多数时间运行良好.但是,由于某些原因,该进程可能没有输出,并且For循环将被Scan()方法阻塞,并且无法从processFinishChan接收.

有没有简单的方法来设置Scan()方法的超时时间?我try 了一个在另一个Goroutine中运行Scan()的解决方案,并使用另一个SELECT从新的Goroutine和一个超时通道接收,但考虑到外部for循环,是否会有越来越多的Goroutine被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将在看到命令退出后关闭管道,因此大多数调用者不需要自己关闭管道.

因此,我们需要在一个单独的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()
}

Go相关问答推荐

Golang Cososdb-gremlin连接

try 用GitHub操作中的release标签更新version. go文件,但失败了

golang 的通用 map 功能

Go SQLCMD比Windows本机版本慢吗?

理解Golang中的IOTA和常量

go grpc:无法导入github.com/golang/protobuf/proto(没有所需的模块提供包github.com/gorang/protobuf-proto)

在 GoLang 中对自定义 struct 体数组进行排序

Go Template 动态获取变量

在 Windows 11 上运行 go mod tidy 时的 gitlab 权限问题

Kubo,来自 IpfsNode.Bootstrap 的无效内存地址或零指针取消引用

GoLang:net.LookupHost 返回重复的 ips

如何使用 GolangCI 删除未使用的导入

如何使用 fyne Go 使用 canvas.NewText() 使文本可滚动

Golang计算 struct struct 中的字段数

当有多个同名包时如何在vscode中显示golang包完整路径?

同时调用应该只获取一次数据

curl:(56)Recv失败:连接由golang中的对等方与docker重置

在 Golang 中使用 OR 条件验证 struct 的两个字段

Golang API 的 HTTPS

try 创建新的 etcdv3 客户端时出现pc error: code = Unavailable desc = error reading from server: EOF