parallel

package module
v0.0.0-...-588217f Latest Latest
Warning

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

Go to latest
Published: May 2, 2023 License: MIT Imports: 4 Imported by: 0

README

What

Simple module for one-off fan-out workloads. Akin to C's OpenMP parallelism pragmas.

Install

go get github.com/wit221/go/parallel

Examples

See examples/**/main.go. Basic examples:

For:

func main() {
    ctx := context.Background()

    items := []*In{
        {"Hi"},
        {"Hello"},
        {"Bye"},
        {"Goodbye"},
    }

    workFn := func(ctx context.Context, item *In) error {
        select {
        case <-time.After(time.Second):
        case <-ctx.Done():
            return oops.Errorf("failed to process item %s", item.Text)
        }

        item.Text = fmt.Sprintf("%s processed", item.Text)
        return nil
    }
    err := parallel.For(ctx, items, workFn)
    // ...
}

Map:

func main() {
	ctx := context.Background()

	items := []*In{
		{"Hi"},
		{"Hello"},
		{"Bye"},
		{"Goodbye"},
	}

	delay := NewPtr(1 * time.Second)

	workFn := func(ctx context.Context, item *In) (*Out, error) {
		select {
		case <-time.After(*delay):
		case <-ctx.Done():
			return nil, oops.Errorf("failed to process item %s", item.Text)
		}

		return &Out{ProcessedText: fmt.Sprintf("%s processed", item.Text)}, nil
	}

	res, err := parallel.Map(ctx, items, workFn)
    // ...
}

Documentation

Index

Constants

View Source
const (
	DefaultParallelism = 100
	DefaultTimeout     = 60 * time.Second
)

Variables

This section is empty.

Functions

func For

func For[T any](
	ctx context.Context,
	items []T,
	fn ForFn[T],
	options ...Option,
) error

For performs work for each item in the input slice in parallel. It performs work for all items and returns the first encountered error. In practice, execution of all subsequent items will be cancelled right away because the context is cancelled upon first error.

func Map

func Map[T any, T2 any](
	ctx context.Context,
	items []T,
	fn MapFn[T, T2],
	options ...Option,
) ([]T2, error)

Map maps input items to output items in parallel. It performs work for all items and returns a results slice and the first encountered error. In practice, execution of all subsequent items will be cancelled right away because the context is cancelled upon first error.

Types

type ForFn

type ForFn[T any] func(ctx context.Context, item T) error

type MapFn

type MapFn[T any, T2 any] func(ctx context.Context, item T) (T2, error)

type Option

type Option func(*opts)

func WithParallelism

func WithParallelism(parallelism int) Option

func WithTimeout

func WithTimeout(timeout time.Duration) Option

Directories

Path Synopsis
examples
for
map

Jump to

Keyboard shortcuts

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