thread

package
v0.0.0-...-b1b21d8 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2023 License: LGPL-3.0 Imports: 8 Imported by: 0

README

Function Overview

  • Lightning-fast thread pool

Multi-threading Example

func main() {
    pool := thread.NewClient(nil, 3)
    for i := 0; i < 20; i++ {
        _, err := pool.Write(&thread.Task{
            Func: func(ctx context.Context, i int) {
                log.Print(i, "start")
                time.Sleep(time.Second)
                log.Print(i, "end")
            },
            Args: []any{i},
        })
        if err != nil {
            log.Panic(err)
        }
    }
    pool.Join()
    log.Print("Finished")
}

Get Thread ID

package main

import (
    "context"
    "log"
    "time"

    "gitee.com/baixudong/gospider/thread"
)

func test(ctx context.Context, num int) {
    log.Printf("Thread %d in thread pool %d starts", thread.GetThreadId(ctx), thread.GetThreadPoolId(ctx))
    time.Sleep(time.Second)
    log.Printf("Thread %d in thread pool %d ends", thread.GetThreadId(ctx), thread.GetThreadPoolId(ctx))
}

func main() {
    threadCli := thread.NewClient(nil, 3) // Limit concurrency to 3
    for i := 0; i < 10; i++ {
        // Write tasks
        threadCli.Write(&thread.Task{
            Func: test,
            Args: []any{i},
        })
    }
    threadCli.Join()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrPoolClosed = errors.New("pool closed")
View Source
var ThreadId myInt = 0

Functions

func GetThreadId

func GetThreadId(ctx context.Context) int64

Types

type BaseClientOption

type BaseClientOption[T any] struct {
	Debug               bool                                    //是否显示调试信息
	ThreadStartCallBack func(context.Context, int64) (T, error) //每一个线程开始时,根据线程id,创建一个局部对象
	ThreadEndCallBack   func(context.Context, T) error          //线程被消毁时的回调,再这里可以安全的释放局部对象资源
	TaskCallBack        func(*Task) error                       //有序的任务完成回调
}

type Client

type Client[T any] struct {
	// contains filtered or unexported fields
}

func NewBaseClient

func NewBaseClient[T any](preCtx context.Context, maxNum int64, options ...BaseClientOption[T]) *Client[T]

func (*Client[T]) Close

func (obj *Client[T]) Close()

func (*Client[T]) Done

func (obj *Client[T]) Done() <-chan struct{}

func (*Client[T]) Empty

func (obj *Client[T]) Empty() bool

func (*Client[T]) Err

func (obj *Client[T]) Err() error

func (*Client[T]) Join

func (obj *Client[T]) Join() error

func (*Client[T]) ThreadSize

func (obj *Client[T]) ThreadSize() int64

func (*Client[T]) Write

func (obj *Client[T]) Write(task *Task) (*Task, error)

创建task

type ClientOption

type ClientOption = BaseClientOption[bool]

type DefaultClient

type DefaultClient = Client[bool]

func NewClient

func NewClient(preCtx context.Context, maxNum int64, options ...ClientOption) *DefaultClient

type Task

type Task struct {
	Func     any                                //运行的函数
	Args     []any                              //传入的参数
	CallBack func(context.Context, []any) error //回调函数
	Timeout  time.Duration                      //超时时间
	Result   []any                              //函数执行的结果
	Error    error                              //函数错误信息
	// contains filtered or unexported fields
}

func (*Task) Done

func (obj *Task) Done() <-chan struct{}

Jump to

Keyboard shortcuts

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