登录
首页 >  Golang >  Go问答

如何有效停止两个goroutine?

来源:stackoverflow

时间:2024-03-12 13:39:25 394浏览 收藏

积累知识,胜过积蓄金银!毕竟在Golang开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《如何有效停止两个goroutine?》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

我正在使用两个并发 goroutine 将 stdin/stdout 从我的终端复制到 net.conn 目标。由于某种原因,我无法在不出现恐慌错误(尝试关闭已关闭的连接)的情况下完全停止这两个 go 例程。这是我的代码:

func interact(c net.Conn, sessionMap map[int]net.Conn) {
    quit := make(chan bool) //the channel to quit

    copy := func(r io.ReadCloser, w io.WriteCloser) {
        defer func() {
            r.Close()
            w.Close()
            close(quit) //this is how i'm trying to close it
        }()

        _, err := io.Copy(w, r)

        if err != nil {
            //
        }

    }

    go func() {
        for {
            select {
            case <-quit:
                return
            default:
                copy(c, os.Stdout)
            }
        }
    }()

    go func() {
        for {
            select {
            case <-quit:
                return
            default:
                copy(os.Stdin, c)
            }
        }
    }()
}

此错误为 panic:关闭已关闭通道 弯弯弯弯 我想终止这两个 go 例程,然后正常继续执行另一个函数。我做错了什么?


解决方案


您不能在一个通道上多次调用 close ,没有理由在 for 循环中调用 copy ,因为它只能操作一次,并且您复制方向错误,写入 stdin 并读取从标准输出。

简单地询问如何退出 2 个 goroutine 很简单,但这并不是您在这里需要做的唯一事情。由于 io.copy 是阻塞的,因此您不需要额外的同步来确定调用何时完成。这可以让您显着简化代码,从而更容易推理。

func interact(c net.conn) {
    go func() {
        // you want to close this outside the goroutine if you
        // expect to send data back over a half-closed connection
        defer c.close()

        // optionally close stdout here if you need to signal the
        // end of the stream in a pipeline.
        defer os.stdout.close()

        _, err := io.copy(os.stdout, c)
        if err != nil {
            //
        }
    }()

    _, err := io.copy(c, os.stdin)
    if err != nil {
        //
    }
}

另请注意,您可能无法从标准输入中突破 io.copy,因此您不能期望 interact 函数返回。在函数体中手动执行 io.copy 并在每个循环上检查半关闭连接可能是一个好主意,这样您就可以更快地突破并确保完全关闭 net.conn

也可以这样

func scanReader(quit chan int, r io.Reader) chan string {
          line := make(chan string)
           go func(quit chan int) {
                    defer close(line)
                    scan := bufio.NewScanner(r)
                    for scan.Scan() {
                    select {
                       case <- quit:
                          return
                       default:
                          s := scan.Text()
                          line <- s
                    }
                    }
                }(quit)
               return line
    }

            stdIn := scanReader(quit, os.Stdin) 
            conIn := scanReader(quit, c) 
        for {
            select {
                    case <-quit:
                        return
                    case l <- stdIn:
                        _, e := fmt.Fprintf(c, l)
                        if e != nil {
                           quit <- 1
                           return
                        }
                    case l <- conIn:
                        fmt.Println(l)
                    }
        }

理论要掌握,实操不能落!以上关于《如何有效停止两个goroutine?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>