Lightweight Goroutine pool
Clients can submit jobs. Dispatcher takes job, and sends it to first available worker. When worker is done with processing job, will be returned back to worker pool.
Number of workers and Job queue size is configurable.
https://godoc.org/github.com/ivpusic/grpool
go get github.com/ivpusic/grpool
package main
import (
"fmt"
"runtime"
"time"
"github.com/ivpusic/grpool"
)
func main() {
numCPUs := runtime.NumCPU()
runtime.GOMAXPROCS(numCPUs)
// number of workers, and size of job queue
pool := grpool.NewPool(10, 50)
job := grpool.Job{
// define function which will be called on worker
Fn: func(arg interface{}) {
fmt.Printf("hello %s\n", arg)
},
// provide arguments
Arg: "world",
}
// submit one or more jobs to pool
for i := 0; i < 10; i++ {
pool.JobQueue <- job
}
// dummy wait until jobs are finished
time.Sleep(1 * time.Second)
}
package main
import (
"fmt"
"runtime"
"github.com/ivpusic/grpool"
)
func main() {
numCPUs := runtime.NumCPU()
runtime.GOMAXPROCS(numCPUs)
// number of workers, and size of job queue
pool := grpool.NewPool(10, 50)
// how many jobs we should wait
pool.WaitCount(10)
job := grpool.Job{
// define function which will be called on worker
Fn: func(arg interface{}) {
// say that job is done, so we can know how many jobs are finished
defer pool.JobDone()
fmt.Printf("hello %s\n", arg)
},
// provide arguments
Arg: "world",
}
// submit one or more jobs to pool
for i := 0; i < 10; i++ {
pool.JobQueue <- job
}
// wait until we call JobDone for all jobs
pool.WaitAll()
}
package main
import (
"fmt"
"runtime"
"github.com/ivpusic/grpool"
)
func main() {
numCPUs := runtime.NumCPU()
runtime.GOMAXPROCS(numCPUs)
// number of workers, and size of job queue
pool := grpool.NewPool(10, 50)
// how many jobs we should wait
pool.WaitCount(10)
job := grpool.Job{
// define function which will be called on worker
Fn: func(arg interface{}) {
// say that job is done, so we can know how many jobs are finished
defer pool.JobDone()
fmt.Printf("hello %s\n", arg)
},
// provide arguments
Arg: "world",
RecoverFn: func() {
fmt.Println("Custom panic catch.")
},
}
// submit one or more jobs to pool
for i := 0; i < 10; i++ {
pool.JobQueue <- job
}
// wait until we call JobDone for all jobs
pool.WaitAll()
}
MIT