parallel

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2022 License: MIT Imports: 4 Imported by: 0

README

Parallel - generic functions for coordinating parallel workers

Go Reference Go Report Card Tests Coverage Status

This is parallel, a collection of generic functions for coordinating parallel workers.

This package includes these functions:

  • Consumers, for managing a set of N workers consuming a stream of values produced by the caller
  • Producers, for managing a set of N workers producing a stream of values consumed by the caller
  • Values, for concurrently producing a set of N values
  • Pool, for managing access to a pool of concurrent workers

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Consumers

func Consumers[T any](ctx context.Context, n int, f func(context.Context, int, T) error) (func(T) error, func() error)

Consumers launches n parallel workers each consuming values supplied by the caller.

When a value is available, an available worker calls the function f to consume it. This callback receives the worker's number (in the range 0 through n-1) and the value.

An error from any worker cancels them all.

The caller receives two callbacks: one for sending a value to the workers, and one for closing that channel (signaling the end of input and causing the workers to exit normally).

func Pool

func Pool[T, U any](n int, f func(T) (U, error)) func(T) (U, error)

Pool permits up to n concurrent calls to a function f. The caller receives a callback for requesting a worker from this pool. When no worker is available, the callback blocks until one becomes available. Then it invokes f and returns the result.

Each call of the callback is synchronous. Any desired concurrency is the responsibility of the caller.

func Producers

func Producers[T any](ctx context.Context, n int, f func(context.Context, int, func(T) error) error) func() (T, bool, error)

Producers launches n parallel workers each running the function f.

Each worker receives its worker number (in the range 0 through n-1) and a callback to use for producing a value. If the callback returns an error, the worker should exit with that error.

An error from any worker cancels them all.

The caller gets a callback for consuming the values produced. Each call of the callback yields a value, a boolean, and an error. If the error is non-nil, it has propagated out from one of the workers and no more data may be consumed. Otherwise, if the boolean is false, the workers have completed and there is no more output to consume. Otherwise, the value is a valid next value.

Example:

consume := Producers(ctx, 10, produceVals)
for {
  val, ok, err := consume()
  if err != nil { panic(err) }
  if !ok { break }
  ...handle val...
}

func Values

func Values[T any](ctx context.Context, n int, f func(context.Context, int) (T, error)) ([]T, error)

Values produces a slice of n values using n parallel workers each running the function f.

Each worker receives its worker number (in the range 0 through n-1).

An error from any worker cancels them all. The first error is returned to the caller.

The resulting slice has length n. The value at position i comes from worker i.

Types

type Error

type Error struct {
	N   int
	Err error
}

Error is an error type for wrapping errors returned from worker goroutines. It contains the worker number of the goroutine that produced the error.

func (Error) Error

func (e Error) Error() string

func (Error) Unwrap

func (e Error) Unwrap() error

Jump to

Keyboard shortcuts

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