go-worker-pool
![Go Report Card](https://camo.githubusercontent.com/6ea6c8ffbd3593b06fdb3c802556d569e889dd05052aa3cd721bcdb0d734cc42/68747470733a2f2f676f7265706f7274636172642e636f6d2f62616467652f6769746875622e636f6d2f6a61796875616e6737352f676f2d776f726b65722d706f6f6c)
Thank you for all the good libraries and Articles:
- Tunny
- Wpool
- Handling 1 millon requests per minute with golang
- Visually understanding worker pool
How to use this?
Install package
$ go get github.com/jayhuang75/go-worker-pool
In your application main.go, import the package
import (
"github.com/jayhuang75/go-worker-pool"
)
Example how to use the worker pool
// Person Struct
type Person struct {
Name string
Age int
}
// Processor func
// This is the custom process function
func Processor(resource interface{}) error {
// fmt.Printf("worker: started, working for %s\n", resource)
fmt.Printf(">>>>>>>>>>>>>> %s \n", resource.(Person).Name+" ok")
return nil
}
// ResultProcessor func
// We can catch all the failed and retry in here
func Result(result worker.Result) error {
fmt.Printf("Result processor got error: %s\n", result.Err)
fmt.Printf("Result processor got result: %d\n", result.Job)
return nil
}
func main() {
p1 := Person{"apple ", 3}
p2 := Person{"orange", 8}
p3 := Person{"pear", 35}
p4 := Person{"pizza", 3}
p5 := Person{"cafe", 8}
persons := []Person{p1, p2, p3, p4, p5}
numCPUs := runtime.NumCPU()
// convert the Struct to the interface
resources := make([]interface{}, len(persons))
for i, s := range persons {
resources[i] = s
}
pool := worker.NewWorkerPool(numCPUs)
pool.Log(false) // remove heavry logging
pool.Start(resources, Processor, Result)
}