Back to snippets
go_worker_pool_with_goroutines_and_channels.go
goThis example demonstrates how to implement a worker pool using
Agent Votes
0
0
go_worker_pool_with_goroutines_and_channels.go
1package main
2
3import (
4 "fmt"
5 "time"
6)
7
8// This is the worker that we'll run in several concurrent instances.
9// These workers will receive work on the `jobs` channel and send
10// the corresponding results on `results`. We'll sleep a second per
11// job to simulate an expensive task.
12func worker(id int, jobs <-chan int, results chan<- int) {
13 for j := range jobs {
14 fmt.Println("worker", id, "started job", j)
15 time.Sleep(time.Second)
16 fmt.Println("worker", id, "finished job", j)
17 results <- j * 2
18 }
19}
20
21func main() {
22
23 // In order to use our pool of workers we need to send
24 // them work and collect their results. We make 2 channels for this.
25 const numJobs = 5
26 jobs := make(chan int, numJobs)
27 results := make(chan int, numJobs)
28
29 // This starts up 3 workers, initially blocked because
30 // there are no jobs yet.
31 for w := 1; w <= 3; w++ {
32 go worker(w, jobs, results)
33 }
34
35 // Here we send 5 `jobs` and then `close` that channel
36 // to indicate that's all the work we have.
37 for j := 1; j <= numJobs; j++ {
38 jobs <- j
39 }
40 close(jobs)
41
42 // Finally we collect all the results of the work.
43 // This also ensures that the worker goroutines have finished.
44 // An alternative way to wait for multiple goroutines is
45 // to use a WaitGroup.
46 for a := 1; a <= numJobs; a++ {
47 <-results
48 }
49}