Worker Pool Over a Channel
Dispatch jobs to a fixed pool of workers through channels, bounding concurrency while processing a large queue of tasks.
Code
package main
import (
"fmt"
"sync"
)
func worker(id int, jobs <-chan int, results chan<- int, wg *sync.WaitGroup) {
defer wg.Done()
for job := range jobs {
results <- job * job
}
}
func runPool(items []int, workers int) []int {
jobs := make(chan int, len(items))
results := make(chan int, len(items))
var wg sync.WaitGroup
for w := 0; w < workers; w++ {
wg.Add(1)
go worker(w, jobs, results, &wg)
}
for _, item := range items {
jobs <- item
}
close(jobs)
wg.Wait()
close(results)
out := make([]int, 0, len(items))
for result := range results {
out = append(out, result)
}
return out
}
func main() {
fmt.Println(runPool([]int{1, 2, 3, 4}, 2))
}Report a problem with this exampleOpens a pre-filled issue on GitHub.