Created
July 2, 2021 05:24
-
-
Save akshaybharambe14/0a150a8098d11d809757e284bd4e5182 to your computer and use it in GitHub Desktop.
Simple worker pool in golang/ https://play.golang.org/p/6SRS2KScnuu
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
// https://play.golang.org/p/6SRS2KScnuu | |
package main | |
import ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
const ( | |
numJobs = 30 | |
numWrks = 4 | |
) | |
func main() { | |
run() | |
} | |
func run() { | |
var ( | |
// we should have just enough buffer so that work producer should not block | |
in = make(chan int, numWrks) | |
out = make(chan int, numWrks) | |
wg sync.WaitGroup | |
) | |
// create a pool of wrokers | |
for i := 0; i < numWrks; i++ { | |
wg.Add(1) | |
go worker(i, in, out, &wg) | |
} | |
// produce some work in background | |
go func() { | |
for i := 0; i < numJobs; i++ { | |
in <- i // this could block if we exhaust the buffer, hence this producer must process in background | |
} | |
close(in) // signal that we have finished producing work, so that all workers will return | |
}() | |
// wait for all workers to finish | |
go func() { | |
wg.Wait() | |
// all workers returned, we can safely close the out chan | |
close(out) // signal that we have finished producing output | |
}() | |
for i := range out { | |
fmt.Printf("received %d\n", i) | |
} | |
fmt.Println("exiting...") | |
} | |
func worker(id int, in <-chan int, out chan<- int, wg *sync.WaitGroup) { | |
for i := range in { | |
out <- work(i) | |
fmt.Printf("worker id %d processed %d\n", id, i) | |
} | |
fmt.Printf("worker id %d finished working\n", id) | |
wg.Done() // signal that this worker has finished working | |
} | |
func work(i int) int { | |
time.Sleep(time.Second) | |
return i | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment