What is the neatest idiom for producer/consumer in Go?

后端 未结 5 2067
不知归路
不知归路 2021-02-01 23:42

What I would like to do is have a set of producer goroutines (of which some may or may not complete) and a consumer routine. The issue is with that caveat in parentheses - we do

5条回答
  •  孤街浪徒
    2021-02-02 00:32

    producer-consumer is such a common pattern that I write a library prosumer for convenience with dealing with chan communication carefully. Eg:

    func main() {
        maxLoop := 10
        var wg sync.WaitGroup
        wg.Add(maxLoop)
        defer wg.Wait()
    
        consumer := func(ls []interface{}) error {
            fmt.Printf("get %+v \n", ls)
            wg.Add(-len(ls))
            return nil
        }
    
        conf := prosumer.DefaultConfig(prosumer.Consumer(consumer))
        c := prosumer.NewCoordinator(conf)
        c.Start()
        defer c.Close(true)
    
        for i := 0; i < maxLoop; i++ {
            fmt.Printf("try put %v\n", i)
            discarded, err := c.Put(i)
            if err != nil {
                fmt.Errorf("discarded elements %+v for err %v", discarded, err)
                wg.Add(-len(discarded))
            }
            time.Sleep(time.Second)
        }
    
    }
    

    close has a param called graceful, which means whether drain the underlying chan.

提交回复
热议问题