Documentation ¶
Overview ¶
非阻塞协程池,协程数量可动态增长,可配置最大协程并发数量,可手动释放空闲的协程
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrTaskPool = errors.New("naza.taskpool: fxxk")
Functions ¶
func KillIdleWorkers ¶
func KillIdleWorkers()
Types ¶
type Pool ¶
type Pool interface { // 向池内放入任务 // 非阻塞函数,不会等待task执行 Go(task TaskFn, param ...interface{}) // 获取当前的状态,注意,只是一个瞬时值 GetCurrentStatus() Status // 关闭池内所有的空闲协程 KillIdleWorkers() }
func NewPool ¶
Example ¶
并发计算0+1+2+...+1000 演示怎么向协程池中添加带参数的函数任务
package main import ( "fmt" "sync" "sync/atomic" "github.com/q191201771/naza/pkg/taskpool" ) func main() { pool, _ := taskpool.NewPool(func(option *taskpool.Option) { // 限制最大并发数 option.MaxWorkerNum = 16 }) var sum int32 var wg sync.WaitGroup n := 1000 wg.Add(n) for i := 0; i < n; i++ { pool.Go(func(param ...interface{}) { ii := param[0].(int) atomic.AddInt32(&sum, int32(ii)) wg.Done() }, i) } wg.Wait() fmt.Println(sum) }
Output: 499500
Click to show internal directories.
Click to hide internal directories.