WPool it's a simple background worker pool for Go. You can send tasks to be executed concurrently.
Just run
go get -u github.com/ustrajunior/wpool
You have to start the pool and choose how many workers you want.
wp := wpool.New(200, 200)The pool accepts functions with this signature: func() error. To add a new task to the pool, you can use the Add function and add a wpool.Job like this:
fn := func() error {
// same expensive task
}
wp.Add(wpool.Job{Task: fn, Retry: 5})When an error occurs in the Task, wpool will try to execute again until the limit determined in Retry option.
Now, we need to wait for all tasks to finish, so:
wp.Wait()With this simple 3 steps, you can run functions concurrently.
package main
import (
"fmt"
"time"
"github.com/ustrajunior/wpool"
)
func main() {
wp := wpool.New(200, 100)
for i := 0; i < 10; i++ {
a := i
wp.Add(wpool.Job{Retry: 5, Task: func() error {
time.Sleep(200 * time.Millisecond)
fmt.Printf("Running task: %v\n", a+1)
return nil
}})
}
wp.Wait()
}You still need to think about race conditions. If your task access or modify the same variable, you can have a problem.
Copyright (c) 2017-present José Carlos Ustra Júnior
Licensed under MIT License