当前位置: > > > > 根据停止条件,GO 例程永远不会退出 – 无法找到原因
来源:stackoverflow
2024-05-01 22:48:37
0浏览
收藏
小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《根据停止条件,GO 例程永远不会退出 – 无法找到原因》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!
问题内容
在这个例子中,我们有一个工人。这里的想法是根据条件模拟所有 go 例程的 clean shutdown
。
在这种情况下,go 例程会根据工人数量进行旋转。每个 go 例程都会读取通道,执行一些工作并将输出发送到 outputchannel
。
主 go 例程读取此输出并打印它。为了模拟停止条件,关闭 donechannel
。预期的结果是每个 go 例程中的 select
都会选择它并执行 return
,而 return
又会调用 defer println
。实际的输出是它永远不会被调用并且 main 退出。
不知道这背后的原因是什么。
package main import ( "log" "time" ) const jobs = 100 const workers = 1 var timeout = time.After(5 * time.Second) func main() { doneChannel := make(chan interface{}) outputChannel := make(chan int) numberStream := generator() for i := 1; i <= workers; i++ { go worker(doneChannel, numberStream, outputChannel) } // listen for output loop: for { select { case i := <-outputChannel: log.Println(i) case <-timeout: // before you timeout cleanup go routines break loop } } close(doneChannel) time.Sleep(5 * time.Second) log.Println("main exited") } func generator() <-chan int { defer log.Println("generator completed !") c := make(chan int) go func() { for i := 1; i <= jobs; i++ { c <- i } defer close(c) }() return c } func worker(done <-chan interface{}, c <-chan int, output chan<- int) { // this will be a go routine // Do some work and send results to output Channel. // Incase if the done channel is called kill the go routine. defer log.Println("go routines exited") for { select { case <-done: log.Println("here") return case i := <-c: time.Sleep(1 * time.Second) // worker delay output <- i * 100 } } }
正确答案
当主循环在超时期间完成时,您将继续执行程序并
- 关闭已完成频道
- 打印消息
- 退出
没有理由等待任何 goroutine 处理该通道的信号。
如果您添加一个小睡眠,您会看到一些消息
在实际场景中,我们使用等待组来确保所有 goroutine 正确完成
今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注公众号,一起学习编程~