2 Star 3 Fork 0

夏午茶 / sgpool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

sgpool

介绍

协程池

架构

提供两种协程池,可复用/动态创建销毁,debug模式下可以对任务的超时,失败进行统计

安装

go get gitee.com/xiawucha365/sgpool

使用

具体的worker 需要实现 WorkerInterface 接口

type WorkerInterface interface {
	Task() error
	GetTaskID() interface{}
}
常驻复用协程池
package main

import (
	"fmt"
	"math/rand"
	"gitee.com/xiawucha365/sgpool"
	"time"
)


//======================woker实现===start=====================\\
type workersp struct {
	ID string
}

//要执行的任务列表

var name_slices_sp = []string{"001","002","003","004","005","006","007","008","009"}


func (m *workersp) Task() error {

	//fmt.Println("job:" + m.ID + "runing...")
	timen := rand.Intn(3)
	//fmt.Println(timen,"seconds")
	time.Sleep(time.Second * time.Duration(timen))
	fmt.Println("job:" + m.ID + "over")
	return nil
}


//获取任务id,便于
func (m *workersp) GetTaskID() interface{} {
	return m.ID
}

//======================woker实现===end=====================\\
//例子演示
func main() {

	//创建协程池
    //timeout=0 关闭超时统计 debug=true 打开模式
	spool := sgpool.NewSPool(3, cap(name_slices_sp),2,true)

	//提交任务
	for _, id := range name_slices_sp {
		np := workersp{ID: id}
		spool.Commit(&np)
	}

	spool.Release()
	time.Sleep(time.Second * 1)
}
动态创建销毁协程池
package main

import (
	"fmt"
	"math/rand"
	"gitee.com/xiawucha365/sgpool"
	"time"
)

//======================woker实现===start=====================\\
type worker struct {
	ID string
}

//要执行的任务列表

var name_slices = []string{"001", "002", "003", "004", "005", "006", "007", "008", "009", "010", "011"}

func (m *worker) Task() error {

	fmt.Println("job:" + m.ID + "runing...")
	timen := rand.Intn(3)
	//fmt.Println(timen,"seconds")
	time.Sleep(time.Second * time.Duration(timen))
	fmt.Println("job:" + m.ID + "over")
	return nil
}

//获取任务id,便于
func (m *worker) GetTaskID() interface{} {
	return m.ID
}

//======================woker实现===end=====================\\

//并发池实例
var wpool *sgpool.WPool

//例子演示
func main() {

	//创建协程池
    //timeout=0 关闭超时统计 debug=true 打开模式
	wpool = sgpool.NewWPool(100, cap(name_slices), 0, true)

	//提交任务
	for _, id := range name_slices {
		np := worker{ID: id}
		wpool.Commit(&np)
	}

	wpool.Release()

}
MIT License Copyright (c) 2019 夏午茶 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.

简介

协程池,提供两种协程池,可复用/动态创建销毁,debug模式下可以对任务的超时,失败进行统计 展开 收起
Go
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Go
1
https://gitee.com/xiawucha365/sgpool.git
git@gitee.com:xiawucha365/sgpool.git
xiawucha365
sgpool
sgpool
master

搜索帮助