GitXplorerGitXplorer
h

spara

public
5 stars
0 forks
0 issues

Commits

List of commits on branch master.
Unverified
17c0239a70acd08348eda55ba3e04c44c2e4d122

RunWithContext -> spara.RunWithContext

hheyimalex committed 6 years ago
Unverified
4f796bea43a2593e979895f33b06eb175c113b7d

Make docs friendlier

hheyimalex committed 6 years ago
Unverified
03df17958a30d80345875a6c71fc96352e8a397d

Add test for context stopping iteration

hheyimalex committed 6 years ago
Unverified
9397bfd20990988ec4854f7f2d1d7791da373c26

Change how we track parent completion

hheyimalex committed 6 years ago
Unverified
198629d8466c9ff1070795aec8345ca7cfba1d19

Move location of godoc badge

hheyimalex committed 8 years ago
Unverified
942529aa975f1a5ea2cafb8b78435fa7628f9cdc

Fix list formatting in docstring

hheyimalex committed 8 years ago

README

The README file for this repository.

spara GoDoc

Concurrently map over slices in go, with early cancellation on error.

go get github.com/heyimalex/spara

NOTE: This package requires go 1.7+ as it depends on context.

Usage

You have an array of things, and you want to "process" those things concurrently, stopping early if any of those things fails to process. Use spara like this.

const workers = 5                   // Number of worker goroutines to spawn
inputs := []int{1, 2, 3, 4, 5}      // Your things
results := make([]int, len(inputs)) // Place for your results

// Run will call the passed function with every index of the input slice.
spara.Run(workers, len(inputs), func(index int) error {
    input := inputs[index]  // Access the thing
    result := input * 2     // Process the thing
    results[index] = result // Store the result
    return nil              // Return an error if you like
})

fmt.Println(results)
// Output: [2 4 6 8 10]

This package also has support for go's context.Context, so your processing function can know when it's time to quit.

parent, cancel := context.WithTimeout(context.Background(), time.Millisecond * 10)
defer cancel()

err := spara.RunWithContext(parent, 5, 50, func(ctx context.Context, idx int) error {
    <-ctx.Done()
    return ctx.Err()
})

fmt.Println(err)
// Output:  context deadline exceeded

Read more in the godoc.