为何我的“已完成”频道会突然关闭?
来源: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 例程正在运行:
gen
,您的生成器,写入无缓冲的输出通道,直到done
pipeA
,从gen
读取,写入无缓冲的输出通道,直到done
pipeB
,从pipeA
读取,写入无缓冲的输出通道,直到done
main
,从pipeB
读取,直到done
现在,当您关闭 done
时,它完全取决于 go 例程看到它的顺序。
如果main
是第一个看到done
关闭的,它将打破for循环并停止从pipeB
消耗。但如果 pipeB
仍在尝试写入输出通道 (ret <- tmp
),它将在那里阻塞;所以它永远不会到达 <-done
部分。
有两个选项可以解决此问题:
- 仅在生成器中监听
done
,并让其他 go 例程使用for n := range in { }
。 - 将您的发送逻辑也放入
select
中,以便您的生成器和管道可以检测到done
何时关闭。
或者,您可能想要使用缓冲输出通道,但即使如此,此问题仍然可能发生。
本篇关于《为何我的“已完成”频道会突然关闭?》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习