1 Star 14 Fork 0

A-涛 / taskpool

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
contribute
Sync branch
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README
MIT

1. 介绍

  • 支持预分配协程和用多少申请多少
  • 分配的协程都有一个生命周期,生命周期到了会被自动回收
  • 协程池最少有一个哨兵协程,最多有 maxWork + 1(哨兵)
  • 如果要关闭协程池需要手动释放, 异常情况时会自动释放

2. 使用

go get -u gitee.com/xuesongtao/taskpool
    pushPool := lib.NewTaskPool("poolName", 10, lib.WithProGoWorker())
    defer pushPool.SafeClose() // 局部使用需要使用这个进行 Close()

    everyTaskHandleSum := 500
    l := 50000
    pushIds := []string{xxx}
    totalPage := math.Ceil(float64(l) / float64(everyTaskHandleSum))
    for page := 1; page <= int(totalPage); page++ {
        // 根据切片分页
        startIndex, endIndex := lib.GetPapeSliceIndex(int32(page), int32(everyTaskHandleSum), l)
        tmpArr := pushIds[startIndex:endIndex]
        i.log.Infof("startIndex: %d, endIndex: %d, total: %d", startIndex, endIndex, l)

        // 阻塞式
        pushPool.Submit(func() {
            i.PushBatchMsg(tmpArr, info)
        })
    }
  • 其他: 在使用时需要注意闭包引用问题, 在 _example 下添加使用示例

最后

  • 欢迎大佬们指正, 希望大佬给 star
MIT License Copyright (c) 2022 A-涛 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

1 . 支持预分配协程和用多少申请多少 2. 分配的协程都有一个生命周期,生命周期到了会被自动回收 3. 协程池最少有一个哨兵协程,最多有 maxWork + 1(哨兵) expand collapse
Go
MIT
Cancel

Releases

No release

Contributors

All

Activities

Load More
can not load any more
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xuesongtao/taskpool.git
git@gitee.com:xuesongtao/taskpool.git
xuesongtao
taskpool
taskpool
master

Search

344bd9b3 5694891 D2dac590 5694891