parallelize

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2023 License: MIT Imports: 2 Imported by: 6

README

parallelize-go

Parallelize functions in golang

This library works in few modes: a) for doing short io bound work default, it spawns 2x goroutines of CPU. b) If its unsuitable parallel count can be specified. c) for running long-running io bound work provides a function to spawns unbounded goroutine.

It returns early with the first error it sees. note, first error it sees from parallel goroutine and not first error as in parallel many goroutines may simultaneously return an error.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MultiParamWork

type MultiParamWork struct {
	Data []interface{}
	Func func(...interface{}) (interface{}, error)
}

MultiParamWork is a utility for doing a multi param work

func (*MultiParamWork) Work

func (mpw *MultiParamWork) Work() (interface{}, error)

Work satisfies Work interface. So we can now pass an anonymous function casted to MultiParamWork

type Runner

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

Runner holds data for initiating parallel work

func NewBoundedRunner

func NewBoundedRunner(workSize, parallelCount int) *Runner

NewBoundedRunner creates bounded goroutines by factor parallel count

func NewFixedBoundedRunner

func NewFixedBoundedRunner(workSize int, ioBound bool) *Runner

NewFixedBoundedRunner creates fixed bounded goroutines by factor of available CPU based on default setting. Default setting is to have parallelization as factor of 4x if iobound work which is supposed to be short. If its a long work use NewBoundedRunner instead with optimal config. If cpu bound work parallel go routine will be bound by cpu.

func NewUnboundedRunner

func NewUnboundedRunner(workSize int) *Runner

NewUnboundedRunner creates unbounded goroutines

func (*Runner) GetResult

func (r *Runner) GetResult() ([]interface{}, error)

GetResult returns array of responses from executing Work and returns early on first error it gets. Also after calling this no more work can be submitted to the Runner

func (*Runner) SubmitWork

func (r *Runner) SubmitWork(wrk Work)

SubmitWork submits a unit of work to be executed

type SingleParamWork

type SingleParamWork struct {
	Data interface{}
	Func func(data interface{}) (interface{}, error)
}

SingleParamWork is a utility for doing a single param work

func (*SingleParamWork) Work

func (spw *SingleParamWork) Work() (interface{}, error)

Work satisfies Work interface. So we can now pass an anonymous function casted to SingleParamWork

type StatelessFunc

type StatelessFunc func() (interface{}, error)

StatelessFunc is defined to simulate anonymous implementation directly lacking in golang. It will avoid creating boilerplate implementation of Work interface

func (StatelessFunc) Work

func (sf StatelessFunc) Work() (interface{}, error)

Work satisfies Work interface. So we can now pass an anonymous function casted to StatelessFunc

type ThreeParamWork

type ThreeParamWork struct {
	Data1 interface{}
	Data2 interface{}
	Data3 interface{}
	Func  func(data1 interface{}, data2 interface{}, data3 interface{}) (interface{}, error)
}

ThreeParamWork is a utility for doing a three param work

func (*ThreeParamWork) Work

func (tpw *ThreeParamWork) Work() (interface{}, error)

Work satisfies Work interface. So we can now pass an anonymous function casted to ThreeParamWork

type TwoParamWork

type TwoParamWork struct {
	Data1 interface{}
	Data2 interface{}
	Func  func(data1 interface{}, data2 interface{}) (interface{}, error)
}

TwoParamWork is a utility for doing a two param work

func (*TwoParamWork) Work

func (tpw *TwoParamWork) Work() (interface{}, error)

Work satisfies Work interface. So we can now pass an anonymous function casted to TwoParamWork

type Work

type Work interface {
	Work() (interface{}, error)
}

Work is a unit of work set to be executed by this Runner

Jump to

Keyboard shortcuts

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