Limiting number of go routines running

后端 未结 1 1467
野的像风
野的像风 2020-12-19 08:21

I have a list of urls to process, but I want to run a maximum number of goroutines at a time. For example, if I have 30 urls, I only want 10 goroutines working in parallel.<

相关标签:
1条回答
  • 2020-12-19 08:47

    Create the desired number of workers instead of one worker per url:

    parallel := flag.Int("parallel", 10, "max parallel requests allowed")
    flag.Parse()
    
    // Workers get URLs from this channel
    urls := make(chan string) 
    
    // Feed the workers with URLs
    go func() {
        for _, u := range flag.Args() {
            urls <- u
        }
        // Workers will exit from range loop when channel is closed
        close(urls)
    }()
    
    var wg sync.WaitGroup
    client := rest.Client{}
    
    results := make(chan string)
    
    // Start the specified number of workers.
    for i := 0; i < *parallel; i++ {
        wg.Add(1)
        go func() {
            defer wg.Done()
            for url := range urls {
                worker(url, client, results)
            }
        }()
    }
    
    // When workers are done, close results so that main will exit.
    go func() {
        wg.Wait()
        close(results)
    }()
    
    for res := range results {
        fmt.Println(res)
    }
    
    0 讨论(0)
提交回复
热议问题