Home > Enterprise >  Golang - How to chain together several channels?
Golang - How to chain together several channels?

Time:11-17

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
    }
}

CodePudding user response:

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
    }
}
  •  Tags:  
  • go
  • Related