Lesson 15 of 60 · Go
Concurrency Patterns: Worker Pools
Duration: 25 minutes
Worker Pool Pattern
This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.
A common pattern for processing many jobs with a limited number of goroutines.
func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
fmt.Printf("worker %d processing %d\n", id, j)
results <- j * 2
}
}
func main() {
const numWorkers = 3
jobs := make(chan int, 100)
results := make(chan int, 100)
for w := 1; w <= numWorkers; w++ {
go worker(w, jobs, results)
}
for j := 1; j <= 5; j++ { jobs <- j }
close(jobs)
for a := 1; a <= 5; a++ { fmt.Println(<-results) }
}
Cancellation with context
ctx, cancel := context.WithCancel(context.Background())
go func() { time.Sleep(2 * time.Second); cancel() }()
select {
case <-ctx.Done(): fmt.Println("canceled")
case v := <-results: fmt.Println(v)
}