Golang – 如何将多个通道链接在一起?

huangapple go评论98阅读模式
英文:

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 (
&quot;fmt&quot;
&quot;time&quot;
)
func main() {
numbers := []int{1, 2, 4}
for _, n := range numbers {
fmt.Printf(&quot;Schedule and delete %d items.\n&quot;, n)
statusChan := make(chan string, n)
deleteChan := make(chan string, n)
done := make(chan bool)
go func(n int, statusChan chan&lt;- string) {
for i := 0; i &lt; n; i++ {
fmt.Printf(&quot;Scheduling item number ... %d\n&quot;, i)
itemNum := fmt.Sprintf(&quot;item_num_%d\n&quot;, i)
time.Sleep(500 * time.Millisecond)
statusChan &lt;- itemNum
}
statusChan &lt;- &quot;***&quot;
}(n, statusChan)
go func(statusChan &lt;-chan string, deleteChan chan&lt;- string) {
for itemNum := range statusChan {
fmt.Printf(&quot;Checking status of item number ... %s\n&quot;, itemNum)
deleteChan &lt;- itemNum
}
}(statusChan, deleteChan)
go func(deleteChan &lt;-chan string, done chan&lt;- bool) {
for itemNum := range deleteChan {
if itemNum == &quot;***&quot; {
break
}
fmt.Printf(&quot;Delete item: %s&quot;, itemNum)
}
fmt.Printf(&quot;Done with scheduling and deleting %d item.\n&quot;, n)
done &lt;- true
}(deleteChan, done)
&lt;-done
}
}

huangapple
  • 本文由 发表于 2022年11月16日 05:12:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/74452224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定