Created
August 30, 2019 21:38
-
-
Save digitaldreamer/3b9a6e392b9f9a9bb0cff1d71c89fffa to your computer and use it in GitHub Desktop.
Worker Pool example
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" | |
"log" | |
"math/rand" | |
"sync" | |
"time" | |
) | |
var settings = struct { | |
workers int | |
}{ | |
workers: 10, | |
} | |
func main() { | |
jobNum := 100 | |
// setup channel an wg | |
jobChan := make(chan string, jobNum) | |
wg := &sync.WaitGroup{} | |
// set up jobs: push into channel to queue them to be processed | |
for i := 0; i < jobNum; i++ { | |
wg.Add(1) | |
jobChan <- fmt.Sprintf("job number %d", i) | |
} | |
close(jobChan) | |
// set up X number workers to process jobs | |
for i := 0; i < settings.workers; i++ { | |
go func(wg *sync.WaitGroup, workerNum int, jc chan string) { | |
log.Println("worker starting:", workerNum) | |
for job := range jc { | |
log.Printf("worker:%d processing job:%v\n", workerNum, job) | |
time.Sleep(time.Duration(rand.Int63n(500)) * time.Millisecond) | |
wg.Done() | |
} | |
log.Println("worker stopping:", workerNum) | |
}(wg, i+1, jobChan) | |
} | |
// wait for them to be finished | |
wg.Wait() | |
log.Println("done") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment