Documentation ¶
Overview ¶
A generic scatter/gather implementation to distribute work among many goroutines
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ScatterGather ¶
type ScatterGather[T any] struct { // contains filtered or unexported fields }
Example ¶
Square a bunch of numbers in parallel
package main import ( "context" "fmt" "sort" "github.com/seveas/scattergather" ) // Square a bunch of numbers in parallel func main() { input := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} ctx := context.Background() sg := scattergather.New[int](int64(len(input))) // Start all the workers for _, i := range input { sg.Run(ctx, square(i)) } // And wait for them to finish output, err := sg.Wait() if err != nil { panic(err) } // Values may be returned in any order, so sort them for correct output sort.Ints(output) fmt.Printf("The squares of %v are %v\n", input, output) } func square(i int) func() (int, error) { return func() (int, error) { return i * i, nil } }
Output:
func New ¶
func New[T any](parallel int64) *ScatterGather[T]
Create a new ScatterGather object that will run at most parallel tasks in parallel. When parallel is 0, the maximum is set to GOMAXPROCS.
func (*ScatterGather[T]) KeepAllResults ¶ added in v1.1.0
func (sg *ScatterGather[T]) KeepAllResults(keep bool)
func (*ScatterGather[T]) Run ¶
func (sg *ScatterGather[T]) Run(ctx context.Context, callable func() (T, error))
Add a piece of work to be run. This will call the callable in a separate goroutine and pass the context and arguments. The result and error returned by this function will be collected and returned from Wait()
func (*ScatterGather[T]) SetParallel ¶ added in v1.2.0
func (sg *ScatterGather[T]) SetParallel(parallel int64)
func (*ScatterGather[T]) Wait ¶
func (sg *ScatterGather[T]) Wait() ([]T, error)
Wait for all subtasks to return. The return value is a list of values returned from all subtasks, excluding any nil that was returned. The returned error is either `nil` to indicate no subtask returned an error or a *ScatteredError containing all errors returned by subtasks.
type ScatteredError ¶
type ScatteredError struct {
Errors []error
}
An error type that represents a collection of errors
func (*ScatteredError) AddError ¶
func (e *ScatteredError) AddError(err error)
Add an error to the collection
func (*ScatteredError) Error ¶
func (e *ScatteredError) Error() string
Returns a string containing all errors, separated by newlines
func (*ScatteredError) HasErrors ¶
func (e *ScatteredError) HasErrors() bool
Whether any errors have been added to this object
func (*ScatteredError) Is ¶
func (e *ScatteredError) Is(target error) bool
ScatteredErrors are identical iff the errors in their collections are identical
Directories ¶
Path | Synopsis |
---|---|
x
|
|
sync/semaphore
Package semaphore provides a weighted semaphore implementation.
|
Package semaphore provides a weighted semaphore implementation. |