-
-
Save vietvudanh/77dd5ad2ee6a576cb43bc5a06e42edbc to your computer and use it in GitHub Desktop.
Worker pool to control concurrency and collect results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
const concurrency = 3 | |
const numTasks = 100 | |
func main() { | |
// put tasks on channel | |
// buffered or unbuffered | |
tasks := make(chan int, 1) | |
// single producers | |
go func() { | |
for j := 1; j <= numTasks; j++ { | |
tasks <- j | |
} | |
close(tasks) | |
}() | |
// waitgroup, and close results channel when work done | |
results := make(chan int) | |
wg := &sync.WaitGroup{} | |
wg.Add(concurrency) | |
// close after wg.Wait() is called | |
go func() { | |
wg.Wait() | |
close(results) | |
}() | |
// spawn [concurrency] consumers | |
for i := 1; i <= concurrency; i++ { | |
go func(id int) { | |
defer wg.Done() | |
for t := range tasks { | |
fmt.Println("worker", id, "processing job", t) | |
results <- t * 2 | |
time.Sleep(time.Second) | |
} | |
}(i) | |
} | |
// loop over results until closed (see above) | |
for r := range results { | |
fmt.Println("result", r) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment