Generic worker pool for Go Language. You can limit the number of go routines which are running in parallel.
https://godoc.org/github.com/vladiacob/go_workerpool
go get github.com/vladiacob/go_workerpool
cd examples
go run example.go
include (
..
pool "github.com/vladiacob/go_workerpool"
..
)
workerPool := pool.New(2, 10)
workerPool.Run()
- maxWorkers: number of workers which are processing in parallel
- maxJobQueue: number of jobs which will be accept in queue
err := workerPool.Add(job1)
if err != nil {
fmt.Println(err)
}
err := workerPool.Stop(true)
if err != nil {
fmt.Println(err)
}
- true: wait until all jobs was processed
- false: close workers immediate
// Job structure
type Job struct {
name string
}
// Work is method which is called by the worker
func (j *Job) Work() error {
fmt.Println("Work: " + j.name)
time.Sleep(1 * time.Second)
return nil
}
func (j *Job) SetWorkerID(ID int) {
j.workerID = ID
}