Created
April 11, 2018 07:18
-
-
Save birowo/b2050d6fecafbb1a07f7594ab4e9c06b to your computer and use it in GitHub Desktop.
golang: worker pool . skrip dari: http://chen-tao.github.io/2017/09/11/Golang-Workers-Job-Queue/
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 ( | |
_ "expvar" | |
"fmt" | |
"math/rand" | |
"sync" | |
"time" | |
) | |
const maxWorkers = 10 | |
type job struct { | |
name string | |
duration time.Duration | |
} | |
func doWork(id int, j job) { | |
fmt.Printf("worker%d: started %s, working for %fs\n", id, j.name, j.duration.Seconds()) | |
time.Sleep(j.duration) | |
fmt.Printf("worker%d: completed %s!\n", id, j.name) | |
} | |
func main() { | |
// channel for jobs | |
jobs := make(chan job) | |
// start workers | |
wg := &sync.WaitGroup{} | |
wg.Add(maxWorkers) | |
for i := 1; i <= maxWorkers; i++ { | |
go func(i int) { | |
defer wg.Done() | |
for j := range jobs { | |
doWork(i, j) | |
} | |
}(i) | |
} | |
// add jobs | |
for i := 0; i < 100; i++ { | |
name := fmt.Sprintf("job-%d", i) | |
duration := time.Duration(rand.Intn(1000)) * time.Millisecond | |
fmt.Printf("adding: %s %s\n", name, duration) | |
jobs <- job{name, duration} | |
} | |
close(jobs) | |
// wait for workers to complete | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment