英文:
Golang - How to chain together several channels?
问题
尝试安排一个项目,将项目发送到一个“状态”通道以获取状态,然后将项目发送到一个“删除”通道以删除项目。我设置了两个通道的大小,并期望在项目被删除完成后会得到“完成”的消息。但是,在执行“删除”之后,但在到达“完成”之前,代码似乎停止了。为什么我会在“删除项目”之后和“完成”之前得到“fatal error: all goroutines are asleep - deadlock!”的错误消息?哪个goroutine正在休眠?
这是一个Go语言的代码示例,它创建了几个goroutine来执行并发任务。在这个例子中,可能会出现死锁的情况,导致所有的goroutine都处于休眠状态,无法继续执行。
英文:
Trying to schedule an item, send the item to a "status" channel to get the status then send the item to a "delete" channel to delete the item. I am setting the size of both channels and expected after the items are finished being deleted I would get to "done". It appears the code is stopping after the "delete" is performed right before getting to "done". Why do I get "fatal error: all goroutines are asleep - deadlock!" right after "Delete Item" and before "Done" ? Which go routine is sleeping?
https://go.dev/play/p/W49niF5xngQ
package main
import (
"fmt"
"time"
)
func main() {
numbers := []int{1, 2, 4}
for _, n := range numbers {
fmt.Printf("Schedule and delete %d items.\n", n)
statusChan := make(chan string, n)
deleteChan := make(chan string, n)
done := make(chan bool)
go func(n int, statusChan chan<- string) {
for i := 0; i < n; i++ {
i := i
go func(n int, statusChan chan<- string) {
fmt.Printf("Scheduling item number ... %d\n", i)
itemNum := fmt.Sprintf("item_num_%d\n", i)
time.Sleep(500 * time.Millisecond)
statusChan <- itemNum
}(n, statusChan)
}
}(n, statusChan)
go func(statusChan <-chan string, deleteChan chan<- string) {
for itemNum := range statusChan {
fmt.Printf("Checking status of item number ... %s\n", itemNum)
time.Sleep(500 * time.Millisecond)
deleteChan <- itemNum
}
}(statusChan, deleteChan)
go func(deleteChan <-chan string, done chan<- bool) {
for itemNum := range deleteChan {
fmt.Printf("Delete item: %s", itemNum)
time.Sleep(500 * time.Millisecond)
}
fmt.Printf("Done with scheduling and deleting %d item.\n", n)
done <- true
}(deleteChan, done)
<-done
}
}
答案1
得分: 1
这似乎是你想要的。我使用"***"作为"全部完成"的信号。
package main
import (
"fmt"
"time"
)
func main() {
numbers := []int{1, 2, 4}
for _, n := range numbers {
fmt.Printf("安排并删除 %d 个项目。\n", n)
statusChan := make(chan string, n)
deleteChan := make(chan string, n)
done := make(chan bool)
go func(n int, statusChan chan<- string) {
for i := 0; i < n; i++ {
fmt.Printf("安排项目编号... %d\n", i)
itemNum := fmt.Sprintf("项目编号_%d\n", i)
time.Sleep(500 * time.Millisecond)
statusChan <- itemNum
}
statusChan <- "***"
}(n, statusChan)
go func(statusChan <-chan string, deleteChan chan<- string) {
for itemNum := range statusChan {
fmt.Printf("检查项目编号... %s 的状态\n", itemNum)
deleteChan <- itemNum
}
}(statusChan, deleteChan)
go func(deleteChan <-chan string, done chan<- bool) {
for itemNum := range deleteChan {
if itemNum == "***" {
break
}
fmt.Printf("删除项目:%s\n", itemNum)
}
fmt.Printf("完成安排和删除 %d 个项目。\n", n)
done <- true
}(deleteChan, done)
<-done
}
}
英文:
This seems to do what you were thinking. I use "***" as an "all done" signal.
package main
import (
"fmt"
"time"
)
func main() {
numbers := []int{1, 2, 4}
for _, n := range numbers {
fmt.Printf("Schedule and delete %d items.\n", n)
statusChan := make(chan string, n)
deleteChan := make(chan string, n)
done := make(chan bool)
go func(n int, statusChan chan<- string) {
for i := 0; i < n; i++ {
fmt.Printf("Scheduling item number ... %d\n", i)
itemNum := fmt.Sprintf("item_num_%d\n", i)
time.Sleep(500 * time.Millisecond)
statusChan <- itemNum
}
statusChan <- "***"
}(n, statusChan)
go func(statusChan <-chan string, deleteChan chan<- string) {
for itemNum := range statusChan {
fmt.Printf("Checking status of item number ... %s\n", itemNum)
deleteChan <- itemNum
}
}(statusChan, deleteChan)
go func(deleteChan <-chan string, done chan<- bool) {
for itemNum := range deleteChan {
if itemNum == "***" {
break
}
fmt.Printf("Delete item: %s", itemNum)
}
fmt.Printf("Done with scheduling and deleting %d item.\n", n)
done <- true
}(deleteChan, done)
<-done
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论