How would you define a pool of goroutines to be executed at once?

后端 未结 3 1992
北荒
北荒 2020-11-30 17:18

TL;DR: Please just go to the last part and tell me how you would solve this problem.

I\'ve begun using Go this morning coming from Python. I want to call a closed-sou

3条回答
  •  孤街浪徒
    2020-11-30 17:34

    A simple approach to throttling (execute f() N times but maximum maxConcurrency concurrently), just a scheme:

    package main
    
    import (
            "sync"
    )
    
    const maxConcurrency = 4 // for example
    
    var throttle = make(chan int, maxConcurrency)
    
    func main() {
            const N = 100 // for example
            var wg sync.WaitGroup
            for i := 0; i < N; i++ {
                    throttle <- 1 // whatever number
                    wg.Add(1)
                    go f(i, &wg, throttle)
            }
            wg.Wait()
    }
    
    func f(i int, wg *sync.WaitGroup, throttle chan int) {
            defer wg.Done()
            // whatever processing
            println(i)
            <-throttle
    }
    

    Playground

    I wouldn't probably call the throttle channel "dummy". IMHO it's an elegant way (it's not my invention of course), how to limit concurrency.

    BTW: Please note that you're ignoring the returned error from cmd.Run().

提交回复
热议问题