Krun is a simple worker queue, which provides an easy way to manage and execute jobs concurrently. It can wait for all jobs to finish, and get the results from the executed jobs. The package can be found at github.com/falmar/krun.
To install the Krun package, simply run:
go get -u github.com/falmar/krun
Here's a basic example of how to use the Krun package:
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/falmar/krun"
)
func main() {
queue := krun.New(&krun.Config{
Size: 5, // number of workers
WaitSleep: time.Microsecond,
})
job := func(ctx context.Context) (interface{}, error) {
time.Sleep(time.Millisecond * 100)
return "Hello, Krun!", nil
}
ctx := context.Background()
resChan := queue.Run(ctx, job)
res := <-resChan
if res.Error != nil {
fmt.Println("Error:", res.Error)
os.Exit(1)
}
queue.Wait(ctx)
fmt.Println("Result:", res.Data.(string))
}
Krun is released under the MIT License. See LICENSE for more information.
- unit test
- go releaser