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

后端 未结 3 1968
北荒
北荒 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:27

    I would spawn 4 worker goroutines that read the tasks from a common channel. Goroutines that are faster than others (because they are scheduled differently or happen to get simple tasks) will receive more task from this channel than others. In addition to that, I would use a sync.WaitGroup to wait for all workers to finish. The remaining part is just the creation of the tasks. You can see an example implementation of that approach here:

    package main
    
    import (
        "os/exec"
        "strconv"
        "sync"
    )
    
    func main() {
        tasks := make(chan *exec.Cmd, 64)
    
        // spawn four worker goroutines
        var wg sync.WaitGroup
        for i := 0; i < 4; i++ {
            wg.Add(1)
            go func() {
                for cmd := range tasks {
                    cmd.Run()
                }
                wg.Done()
            }()
        }
    
        // generate some tasks
        for i := 0; i < 10; i++ {
            tasks <- exec.Command("zenity", "--info", "--text='Hello from iteration n."+strconv.Itoa(i)+"'")
        }
        close(tasks)
    
        // wait for the workers to finish
        wg.Wait()
    }
    

    There are probably other possible approaches, but I think this is a very clean solution that is easy to understand.

提交回复
热议问题