task

package
v0.0.0-...-210eeb1 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

对多线程的支持 主要有以下几个方法 Start:创建一个协程去执行,执行结束后协程关闭 StartByExecutor:在预创建的协程节点中执行 StartByFixExecutor:创建一个协程去执行,协程一旦创建就不会关闭

Index

Examples

Constants

This section is empty.

Variables

View Source
var Config = new(Configuration)

Config 配置

View Source
var ErrCannotFindWorker = errors.New("Cannot find worker ")

Obj 协程管理节点,所有新协程的创建都是由这个节点完成

Functions

func SetObject

func SetObject(o *basic.Object)

SetObject 设置回调方法默认执行节点

Types

type Callable

type Callable interface {
	// Call 需要在另外的协程中执行的方法
	// o 协程节点,就是这个方法执行的节点
	// 方法返回值会传递给回调方法
	Call(o *basic.Object) (ret interface{})
}

type CallableWrapper

type CallableWrapper func(o *basic.Object) (ret interface{})

func (CallableWrapper) Call

func (cw CallableWrapper) Call(o *basic.Object) (ret interface{})

type CompleteNotify

type CompleteNotify interface {
	// Done 回调方法
	// ret Callable 方法的返回值
	Done(ret interface{}, t *Task)
}

type CompleteNotifyWrapper

type CompleteNotifyWrapper func(ret interface{}, t *Task)

func (CompleteNotifyWrapper) Done

func (cnw CompleteNotifyWrapper) Done(ret interface{}, t *Task)

type Configuration

type Configuration struct {
	Options *basic.Options // 协程管理节点配置
	Worker  *WorkerConfig  // 协程节点配置
}

func (*Configuration) Close

func (c *Configuration) Close() error

func (*Configuration) Init

func (c *Configuration) Init() error

func (*Configuration) Name

func (c *Configuration) Name() string

type Task

type Task struct {
	O    *basic.Object // 回调方法执行节点
	Name string        // 任务名称
	// contains filtered or unexported fields
}

Task 任务,需要在另外的协程中处理的方法,通常是一些耗时操作

func New

func New(o *basic.Object, c Callable, cb CompleteNotify, name ...string) *Task

New 创建任务 o 回调方法执行的节点 c 需要并发执行的方法 cb 回调方法 name 任务名称

func (*Task) Start

func (t *Task) Start()

Start 创建一个协程去执行,执行结束后协程关闭

Example
package main

import (
	"fmt"
	"github.com/skeletongo/core/basic"
	"github.com/skeletongo/core/task"
)

func main() {
	ch := make(chan struct{})
	task.New(basic.Root, task.CallableWrapper(func(o *basic.Object) interface{} {
		fmt.Println("1")
		return "2"
	}), task.CompleteNotifyWrapper(func(ret interface{}, t *task.Task) {
		fmt.Println(ret)
		fmt.Println(3)
		ch <- struct{}{}
	})).Start()
	<-ch
}
Output:

1
2
3

func (*Task) StartByExecutor

func (t *Task) StartByExecutor(name string)

StartByExecutor 在预创建的协程节点中执行 name 任务名称,名称相同的任务会在同一个协程中串行执行

func (*Task) StartByFixExecutor

func (t *Task) StartByFixExecutor(name string)

StartByFixExecutor 创建一个协程去执行,协程一旦创建就不会关闭 如果已经有任务名称相同的协程了(已经使用相同的name调用过此方法),就不会再创建新协程 name 任务名称,名称相同的任务会在同一个协程中串行执行

Example
package main

import ()

func main() {

}
Output:

type WorkerConfig

type WorkerConfig struct {
	Options   *basic.Options // 协程节点配置
	WorkerCnt int            // 预创建的协程数量
}

Jump to

Keyboard shortcuts

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