gopool

package module
v1.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 24, 2023 License: Apache-2.0 Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gopool

Go coordinated process pool(协程池)

Usage(latest version)
Steps needed(必要步骤)
// Set operating parameters(设置运行参数)
pool := NewPool(10) // Concurrency number(并发数)
pool.SetTaskNum(1000) // Task number(设置任务总数)


// Set Task Function Template(设置任务函数)
func taskFunc(args any) (any, error) {
	// do something

	// !!! you should return the result so that the callback function can use it(应该返回结果给回调函数使用) !!!
	return args, nil
}

// Set Task Callback Function Template(设置任务结果回调函数)
// The result parameter is passed by the task function output(result参数是任务函数返回的结果)
func callbackFunc(result any) (any, error) {
	// do something
	return result, nil
}

// Alternatively, you can create a custom task structure by implementing the ITask interface, as shown below. (也可以通过实现ITask接口自定义任务结构体)


// Add task to queue(添加任务)
go func() {
	for i := 0; i < 1000000; i++ {
		pool.AddTask(gopool.NewTask(taskFunc, callbackFunc, i))
	}
}()


// Start running(开始运行)
pool.Run()
// or(或者)
go pool.Run()

Optional operation(可选操作)
// Set debugging flag(设置调试开关)
func (p *Pool) SetDebug(debug bool) {
 p.debug = debug
}

// Set a per-task run timeout; the default is  no limit(设置任务执行超时时间,默认没限制)
p.SetTimeout(time.Minute * 1)

// Get all result(获取运行结果)
pool.GetResult()

// Get the total run time(获取总运行时间)
pool.GetRunTime()


// Get the number of tasks done(获取完成任务数)
pool.GetDoneNum()

// Get the number of tasks that were successful(获取成功任务数)
pool.GetSuccessNum()

// Get the number of tasks that were failure(获取失败任务数)
pool.GetFailNum()

// Get the current goroutine number(获取当前运行中的goroutine数量)
pool.GetGoroutineNum()

// Get the current busy worker number(获取当前忙碌worker数量)
pool.GetBusyWorkerNum()

// Get the current idle worker number(获取当前空闲worker数量)
pool.GetIdleWorkerNum()

type TaskFunc func(args any) (any, error)
type CallbackFunc func(result any) (any, error)

task := NewEmptyTask()
task.SetTaskFunc(taskFunc TaskFunc)
task.SetCallbackFunc(callbackFunc CallbackFunc)
task.SetArgs(args any)
task.SetId(id int)
Design your task by implement ITask interface(自定义任务结构体,只需要实现ITask接口即可)
type ITask interface {
 Execute() error
 GetResult() any
}

// for example
type MyTask struct {
 ITask
}

func (m *MyTask) Execute() error {
 fmt.Println("my task running...")
 return nil
}

func (m *MyTask) GetResult() any {
 return 1
}

mytask := &MyTask{}
pool.AddTask(mytask)
...

Documentation

Index

Constants

View Source
const (
	POOL_MAX_CAP = 1000000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CallbackFunc added in v1.1.2

type CallbackFunc func(result any) (any, error)

type CtxFlag added in v1.1.8

type CtxFlag string
var Debug CtxFlag = "debug"
var Timeout CtxFlag = "timeout"

type ITask added in v1.2.0

type ITask interface {
	Execute() error
	GetResult() any
}

type Pool

type Pool interface {
	AddTask(ITask) error
	Run()
	DefaultInit()
	GetRunTime() float64
	GetResult() []any
	SetTaskNum(int) // 设置任务总数
	GetDoneNum() int
	GetFailNum() int
	GetSuccessNum() int
	SetDebug(bool)
	SetTimeout(time.Duration)
	GetGoroutineNum() int // 获取goroutine数量
	GetBusyWorkerNum() int
	GetIdleWorkerNum() int
	// contains filtered or unexported methods
}

func NewPool

func NewPool(cap int) Pool

type Task

type Task struct {
	// contains filtered or unexported fields
}

任务队列

func NewEmptyTask added in v1.2.0

func NewEmptyTask() *Task

func NewTask

func NewTask(taskFunc TaskFunc, callback CallbackFunc, args any) *Task

func (*Task) Execute

func (t *Task) Execute() error

执行任务函数

func (*Task) GetId added in v1.2.0

func (t *Task) GetId() int

func (*Task) GetResult added in v1.2.0

func (t *Task) GetResult() any

func (*Task) SetArgs added in v1.2.0

func (t *Task) SetArgs(args any)

func (*Task) SetCallbackFunc added in v1.2.0

func (t *Task) SetCallbackFunc(f CallbackFunc)

func (*Task) SetId added in v1.2.0

func (t *Task) SetId(id int)

func (*Task) SetTaskFunc added in v1.2.0

func (t *Task) SetTaskFunc(f TaskFunc)

type TaskFunc added in v1.1.2

type TaskFunc func(args any) (any, error)

type TimecoutErr added in v1.1.8

type TimecoutErr error
var TimecoutError TimecoutErr = errors.New("timeout")

type Worker

type Worker struct {
	WorkID int // 当前work id
	// contains filtered or unexported fields
}

func NewWorker

func NewWorker(workID int, task ITask) *Worker

func (*Worker) GetTask added in v1.2.0

func (w *Worker) GetTask() ITask

func (*Worker) Run

func (w *Worker) Run(debug bool, timeout time.Duration) (err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址