登录
首页 >  Golang >  Go问答

为何我的“已完成”频道会突然关闭?

来源:stackoverflow

时间:2024-03-03 22:45:26 193浏览 收藏

学习知识要善于思考,思考,再思考!今天golang学习网小编就给大家带来《为何我的“已完成”频道会突然关闭?》,以下内容主要包含等知识点,如果你正在学习或准备学习Golang,就都不要错过本文啦~让我们一起来看看吧,能帮助到你就更好了!

问题内容

我构建了以下 go 代码。

这个想法是构建一个完成通道和一个生成 int 通道的生成器。 将它们连接到二级管道中 channumbers := pipelineb(完成, pipelinea(完成, gen(完成)))

几秒钟后,取消已完成的通道。 我希望看到生成器和管道的两级取消并返回,但文本“pipex 现在终止”仅随机出现,我真的不明白为什么。 有人有想法吗?

package main
import (
    "fmt"
    "time"
)

func gen(done <-chan interface{}) <-chan int {
    ret := make(chan int)
    cx := 0

    go func() {
        for {
            select {
            case <-done:
                fmt.Println("**Generator Terminates now")
                time.Sleep(2 * time.Second)
                fmt.Println("**Generator has terminated now")
                close(ret)
                return
            case ret <- cx:
                fmt.Printf("Gen : we push %d \n", cx)
                cx = cx + 1
            }
        }
    }()
    fmt.Println("Generator has created and returned its channel")
    return ret
}

func pipea(done <-chan interface{}, in <-chan int) <-chan int {
    ret := make(chan int)

    go func() {
        for {
            select {
            case <-done:
                fmt.Println("**pipeA terminates")
                time.Sleep(2 * time.Second)
                fmt.Println("**pipeA has terminated now")
                close(ret)
                return
            case tmp, ok := (<-in):
                if ok {
                    fmt.Printf("pipeA : we push %d \n", tmp)
                    ret <- tmp
                } else {
                    in = nil
                }
            }
        }
    }()
    return ret
}

func pipeb(done <-chan interface{}, in <-chan int) <-chan int {
    ret := make(chan int)

    go func() {
        for {
            select {
            case <-done:
                fmt.Println("**pipeB terminates")
                time.Sleep(2 * time.Second)
                fmt.Println("**pipeB has terminated now")
                close(ret)
                return
            case tmp, ok := (<-in):
                if ok {
                    fmt.Printf("pipeB : we push %d \n", tmp)
                    ret <- tmp
                } else {
                    in = nil
                }
            }
        }
    }()
    return ret
}

func main() {
    done := make(chan interface{})

    chanNumbers := pipeb(done, pipea(done, gen(done)))
    go func() {
        time.Sleep(2 * time.Second)
        close(done)
    }()

forloop:
    for {
        select {
        case n := <-chanNumbers:
            fmt.Printf("Received in main element : %d\n", n)
        case <-done:
            break forloop
        }
    }

    //end of the main program
    fmt.Println("Sleeping some seconds before termiating")
    time.Sleep(8 * time.Second)
    fmt.Println("exit...")
}

解决方案


您有四个 go 例程正在运行:

  1. gen,您的生成器,写入无缓冲的输出通道,直到 done
  2. pipeA,从 gen 读取,写入无缓冲的输出通道,直到 done
  3. pipeB,从 pipeA 读取,写入无缓冲的输出通道,直到 done
  4. main,从 pipeB 读取,直到 done

现在,当您关闭 done 时,它完全取决于 go 例程看到它的顺序。

如果main是第一个看到done关闭的,它将打破for循环并停止从pipeB消耗。但如果 pipeB 仍在尝试写入输出通道 (ret <- tmp),它将在那里阻塞;所以它永远不会到达 <-done 部分。

有两个选项可以解决此问题:

  1. 仅在生成器中监听 done,并让其他 go 例程使用 for n := range in { }
  2. 将您的发送逻辑也放入 select 中,以便您的生成器和管道可以检测到 done 何时关闭。

或者,您可能想要使用缓冲输出通道,但即使如此,此问题仍然可能发生。

本篇关于《为何我的“已完成”频道会突然关闭?》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

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