parallel

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2022 License: MIT Imports: 3 Imported by: 2

README

Parallel

golang 协程并行库,可以指定并发数量,可以当做简单的队列使用

安装 - installation

go get github.com/qbhy/parallel

使用 - usage

  1. 普通用法
    package tests
    
    import (
        "errors"
        "fmt"
        "github.com/qbhy/parallel"
        "testing"
    )
    
    func TestParallel(t *testing.T) {
        // 最多 10 个协程同时运行
        p := parallel.NewParallel(10)
    
        p.Add(func() interface{} {
            return "执行了"
        })
    
        p.Add(func() interface{} {
            panic(errors.New("报错了"))
        })
    
        fmt.Println(p.Wait())
        //会输出 map[0:执行了 1:报错了]
    }
    
  2. 简单的队列
    package tests
    
    import (
        "fmt"
        "github.com/qbhy/parallel"
        "testing"
        "time"
    )
    
    // 测试优雅退出
    func TestParallelGracefulStop(t *testing.T) {
        p := parallel.NewParallel(2)
    
        go func() {
            i := 0
            for ; i < 10; i++ {
                (func(i int) {
                    if i >= 5 {
                        p.GracefulStop()
                    }
                    result := p.Add(func() interface{} {
                        time.Sleep(time.Second)
                        fmt.Printf("每隔1秒执行一次 %d \n", i)
                        return nil
                    })
    
                    fmt.Printf("添加结果: %v, i: %d\n", result, i)
                })(i)
            }
        }()
    
        fmt.Println(p.Listen())
    }
    

    也可以参考 tests/parallel_test.go 的代码

https://github.com/qbhy/parallel
qbhy0715@qq.com

Documentation

Index

Constants

View Source
const (
	NORMAL = iota
	LISTENING
	STOPPED
	GRACEFUL_STOP
)

Variables

View Source
var (
	StoppedError = errors.New("the process has stopped")
)
View Source
var (
	WorkersStoppedError = errors.New("the process has stopped")
)

Functions

This section is empty.

Types

type Parallel

type Parallel struct {
	Callbacks []func() interface{}
	// contains filtered or unexported fields
}

func NewParallel

func NewParallel(concurrent int) *Parallel

func (*Parallel) Add

func (this *Parallel) Add(callback func() interface{}) error

func (*Parallel) Clear

func (this *Parallel) Clear()

func (*Parallel) GracefulStop added in v1.3.0

func (this *Parallel) GracefulStop()

func (*Parallel) IsStopped added in v1.3.0

func (this *Parallel) IsStopped() bool

func (*Parallel) Listen added in v1.3.0

func (this *Parallel) Listen() (err error)

func (*Parallel) Run

func (this *Parallel) Run() map[int]interface{}

func (*Parallel) Stop added in v1.3.0

func (this *Parallel) Stop()

func (*Parallel) Wait

func (this *Parallel) Wait() (results map[int]interface{})

type ParallelStatus added in v1.3.0

type ParallelStatus int

type Workers added in v1.4.0

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

func NewWorkers added in v1.4.0

func NewWorkers(num int) *Workers

func (*Workers) Handle added in v1.4.0

func (workers *Workers) Handle(job func()) error

func (*Workers) Stop added in v1.4.0

func (workers *Workers) Stop()

Jump to

Keyboard shortcuts

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