https://github.com/yankooo/wasps
wasps is a lightweight goroutine pool for golang, use limited goroutines to achieve multi-task concurrent execution.
https://github.com/yankooo/wasps
go golang golang-library goroutine goroutine-pool
Last synced: 11 months ago
JSON representation
wasps is a lightweight goroutine pool for golang, use limited goroutines to achieve multi-task concurrent execution.
- Host: GitHub
- URL: https://github.com/yankooo/wasps
- Owner: yankooo
- License: mit
- Created: 2020-05-22T16:16:00.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-12-24T07:06:13.000Z (over 4 years ago)
- Last Synced: 2025-04-05T19:06:46.121Z (12 months ago)
- Topics: go, golang, golang-library, goroutine, goroutine-pool
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 27
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# wasps
[](https://travis-ci.com/yankooo/wasps) [](https://goreportcard.com/report/github.com/yankooo/wasps) [](https://codecov.io/gh/yankooo/wasps) [](https://pkg.go.dev/github.com/yankooo/wasps?tab=doc) [](https://github.com/yankooo/wasps/blob/master/LICENSE)
English | [中文](README_ZH.md)
## Introduction
`wasps` is a lightweight goroutine pool that implements scheduling management for multiple goroutines.
## Features:
- Automatic scheduling goroutine.
- Provides commonly-used interfaces: task submission, getting the number of running goroutines, dynamically adjusting the size of the pool, and releasing the pool.
- Provide callback type goroutine pool, serialization work goroutine pool, custom work goroutine pool.
- Support custom work goroutine, support panic processing of task goroutine, and custom pass parameter of closure function.
- Asynchronous mechanism.
## Docs
https://godoc.org/github.com/yankooo/wasps
## Installation
``` go
go get github.com/yankooo/wasps
```
## Use
``` go
package main
import (
"context"
"fmt"
"github.com/yankooo/wasps"
"log"
"sync"
"time"
)
func main() {
// init pool
pool := wasps.NewCallback(5)
defer func() {
pool.Release()
}()
var num = 10
ctx, _ := context.WithTimeout(context.TODO(), 1*time.Second)
var wg sync.WaitGroup
wg.Add(3)
// submit first task
var f1 wasps.DefaultWorkerPayLoad = func(ctx context.Context, args ...interface{}) {
defer wg.Done()
num := args[0].(int)
log.Printf("first submit %d", num*2)
}
pool.SubmitWithContext(ctx, f1, wasps.WithArgs(num))
// submit secod task
var f2 wasps.DefaultWorkerPayLoad = func(ctx context.Context, args ...interface{}) {
defer wg.Done()
num := args[0].(int)
log.Printf("second submit %d", num)
}
pool.Submit(f2, wasps.WithArgs(num), wasps.WithRecoverFn(func(r interface{}) { fmt.Printf("catch panic: %+v\n", r) }))
// submit thrid task
num = 200
var f3 wasps.DefaultWorkerPayLoad = func(ctx context.Context, args ...interface{}) {
defer wg.Done()
log.Printf("third submit %d", num)
}
pool.Submit(f3)
wg.Wait()
time.Sleep(time.Second*3)
// submit single task don't use sync group
pool.Do(func(ctx context.Context, args ...interface{}) {
log.Printf("four submit %+v", args)
})
time.Sleep(time.Hour)
}
```