parallel

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2022 License: MIT Imports: 4 Imported by: 1

README

go-parallel

Build Status

parallel control for go

API

Parallel

Runs task with limit concurrency.

arr := strings.Split("0123456789", "")
fn := func(index int) error{
    fmt.Println(index)
    fmt.Println(arr[index])
    return nil
}

err := Parallel(fn, len(arr), 3)
ParallelLock

Runs task with limit concurrency

arr := strings.Split("0123456789", "")
count := 0
fn := func(index int, rw *sync.RWMutex) error {
    fmt.Println(index)
    fmt.Println(arr[index])
    rw.Lock()
    defer rw.Unlock()
    count++
    return nil
}

err := ParallelLock(fn, len(arr), 3)
EnhancedParallel
arr := strings.Split("0123456789", "")
fn := func(index int) error{
    fmt.Println(index)
    fmt.Println(arr[index])
    return nil
}

err := EnhancedParallel(Option{
    Max: len(arr), 
    limit: 3,
    Task: fn,
    BreakOnError: true,
})
Race

Runs task parallel, when the first task is done, then return the result of it.

err := Race(func() error {
    time.Sleep(time.Second)
    return errors.New("error")
}, func() error {
    // the result of this task will be used
    return nil
})
Some

Runs task parallel, when the count of success task is gt count param, it will return nil. Otherwise it will return error.

err := Some(func(index int) error {
    if index%2 == 0 {
        return nil
    }
    return errors.New("error")
}, 5, 3)
Any

Runs task parallel, when one of task is success, it will return nil. Otherwise it will return error.

err := Any(func(index int) error {
    if index == 4 {
        return nil
    }
    return errors.New("error")
}, 5)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any added in v0.2.0

func Any(fn Task, max int) error

Any runs task function, if one task is success, it will return nil, otherwise returns error.

func EnhancedParallel added in v0.1.0

func EnhancedParallel(opt Option) error

EnhancedParallel runs the task function parallel

func Parallel

func Parallel(fn Task, max int, limit ...int) error

Parallel runs task function parallel

func ParallelLock added in v0.4.0

func ParallelLock(fn LockTask, max int, limit ...int) error

ParallelLock runs lock task function parallel

func Race added in v0.2.0

func Race(tasks ...RaceTask) error

Race runs task function race, it's done when one task has been done

func RaceSucceed added in v0.5.0

func RaceSucceed(tasks ...RaceTask) error

RaceSucceed runs task function race, it's done when one task has been successful or all task done.

func Some added in v0.2.0

func Some(fn Task, max, count int) error

Some returns task function, when success time is >= count, it returns nil, otherwise returns error.

Types

type Errors

type Errors struct {
	Errs []error
	// contains filtered or unexported fields
}

Errors

func (*Errors) Add

func (errs *Errors) Add(err error)

Add add error

func (*Errors) Error

func (errs *Errors) Error() string

func (*Errors) Exists

func (errs *Errors) Exists() bool

Exists check error is exists

type LockTask added in v0.4.0

type LockTask func(index int, rw *sync.RWMutex) error

type Option added in v0.1.0

type Option struct {
	// the size of tasks
	Max int
	// the parallel limit
	Limit int
	// task function
	Task Task
	// lock task function
	LockTask LockTask
	// break the function on error
	BreakOnError bool
}

type RaceTask added in v0.2.0

type RaceTask func() error

type Task

type Task func(index int) error

Jump to

Keyboard shortcuts

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