Golang解决fatal error: all goroutines are asleep - deadlock!
2021/7/14 23:47:54
本文主要是介绍Golang解决fatal error: all goroutines are asleep - deadlock!,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
今天进行一个协程操作demo时总是报错
//workerpool.go package main import ( "fmt" "time" ) //工作线程 func workerPool(jobs <-chan int, results chan<- int) { for j := range jobs { fmt.Println("start job", j) time.Sleep(time.Second) //模拟耗时工作 fmt.Println("finish job", j) results <- j } } func main() { const numJobs = 5 jobs := make(chan int, numJobs) // results := make(chan int, numJobs) // go workerPool(jobs, results) for i := 1; i <= numJobs; i++ { jobs <- i } close(jobs) for r := range results { fmt.Println("results :", r) } }
go run workerpool.go 报错
start job 1 finish job 1 start job 2 results : 1 finish job 2 start job 3 results : 2 finish job 3 start job 4 results : 3 finish job 4 start job 5 results : 4 finish job 5 fatal error: all goroutines are asleep - deadlock! results : 5 goroutine 1 [chan receive]: main.main() workerpool.go:30 +0x19d
asleep就是说协程都在睡觉没做事
问题应该出在遍历jobs上, jobs遍历完没数据可操作了,引发 asleep - deadlock 报错
改进方法:
1、是无限循环输入数据到jobs
2、用waitGroup
方法1的无限循环输入数据就不做介绍了,比较简单
下面展示一下方法2使用waitGroup的方式
package main import ( "fmt" "sync" "time" ) var wg sync.WaitGroup //工作线程 func workerPool(jobs <-chan int, results chan<- int) { for j := range jobs { fmt.Println("start job", j) time.Sleep(time.Second) fmt.Println("finish job", j) results <- j } } func init() { wg = sync.WaitGroup{} } func main() { const numJobs = 5 jobs := make(chan int) // results := make(chan int) // go workerPool(jobs, results) go func() { for r := range results { fmt.Println("results :", r) wg.Done()//接收到数据,表示完成了一份工作 } }() for i := 1; i <= numJobs; i++ { wg.Add(1)//标记开始一份工作 jobs <- i } wg.Wait() }
正常运行结果如下:
start job 1
finish job 1
start job 2
results : 1
finish job 2
start job 3
results : 2
finish job 3
start job 4
results : 3
finish job 4
start job 5
results : 4
finish job 5
results : 5
Process finished with exit code 0
这篇关于Golang解决fatal error: all goroutines are asleep - deadlock!的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-24MongoDB资料:新手入门完全指南
- 2024-12-20go-zero 框架的 RPC 服务 启动start和停止 底层是怎么实现的?-icode9专业技术文章分享
- 2024-12-19Go-Zero 框架的 RPC 服务启动和停止的基本机制和过程是怎么实现的?-icode9专业技术文章分享
- 2024-12-18怎么在golang中使用gRPC测试mock数据?-icode9专业技术文章分享
- 2024-12-15掌握PageRank算法核心!你离Google优化高手只差一步!
- 2024-12-15GORM 中的标签 gorm:"index"是什么?-icode9专业技术文章分享
- 2024-12-11怎么在 Go 语言中获取 Open vSwitch (OVS) 的桥接信息(Bridge)?-icode9专业技术文章分享
- 2024-12-11怎么用Go 语言的库来与 Open vSwitch 进行交互?-icode9专业技术文章分享
- 2024-12-11怎么在 go-zero 项目中发送阿里云短信?-icode9专业技术文章分享
- 2024-12-11怎么使用阿里云 Go SDK (alibaba-cloud-sdk-go) 发送短信?-icode9专业技术文章分享