async

package module
v0.0.0-...-52e5b9c Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2018 License: BSD-2-Clause Imports: 3 Imported by: 3

README

async

Package for convenient async operations

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BlockIter

func BlockIter(Count int, N int, fn func(start, limit int))

Spawns N routines, iterating over [0..Count] items by splitting them into blocks [start..limit), note that item "limit" shouldn't be processed.

Example
package main

import (
	"fmt"

	"github.com/egonelbre/async"
)

func main() {
	input := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	output := make([]int, len(input))
	async.BlockIter(len(input), 2, func(start, limit int) {
		for i, v := range input[start:limit] {
			output[start+i] = v * v
		}
	})

	for i, v := range output {
		fmt.Println(i, input[i], v)
	}
}

func Iter

func Iter(Count int, N int, fn func(i int))

Spawns N routines, iterating over [0..Count) items in increasing order

Example
package main

import (
	"fmt"

	"github.com/egonelbre/async"
)

func main() {
	input := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	output := make([]int, len(input))
	async.Iter(len(input), 2, func(i int) {
		output[i] = input[i] * input[i]
	})

	for i, v := range output {
		fmt.Println(i, input[i], v)
	}
}

func Run

func Run(N int, fn func(id int))

Run N routines and wait for all to complete

Example
package main

import (
	"fmt"
	"sync/atomic"

	"github.com/egonelbre/async"
)

func main() {
	total := int64(0)

	async.Run(8, func(id int) {
		atomic.AddInt64(&total, int64(id))
	})

	fmt.Println(total)
}
Output:
28

func Spawn

func Spawn(N int, fn func(id int), whendone ...func())

Spawns N routines, after each completes runs all whendone functions

Example
package main

import (
	"fmt"

	"github.com/egonelbre/async"
)

func main() {
	work := make(chan int, 3)
	done := make(chan int, 3)

	async.Spawn(3, func(id int) {
		for v := range work {
			done <- v * v
		}
	}, func() {
		close(done)
	})

	for i := 0; i < 5; i += 1 {
		work <- i
	}
	close(work)

	for r := range done {
		fmt.Println(r)
	}
}

Types

type Result

type Result struct {
	Done  <-chan struct{}
	Error <-chan error
}

func All

func All(fns ...func() error) Result

All starts all functions concurrently if any error occurs it will be sent to the Error channel after all functions have terminated the Done channel will get a single value

Example (Failing)
package main

import (
	"fmt"
	"time"

	"github.com/egonelbre/async"
)

func main() {
	result := async.All(
		func() error {
			time.Sleep(100 * time.Millisecond)
			return nil
		},
		func() error {
			time.Sleep(200 * time.Millisecond)
			return fmt.Errorf("CRASH")
		},
	)

	select {
	case err := <-result.Error:
		fmt.Printf("Got an error: %v\n", err)
	case <-result.Done:
		fmt.Printf("Success\n")
	}
}
Example (MultipleErrors)
package main

import (
	"fmt"

	"github.com/egonelbre/async"
)

func main() {
	result := async.All(
		func() error {
			return fmt.Errorf("SPLASH")
		},
		func() error {
			return fmt.Errorf("CRASH")
		},
	)

	for err := range result.Error {
		fmt.Printf("Got an error: %v\n", err)
	}
}
Example (Success)
package main

import (
	"fmt"
	"time"

	"github.com/egonelbre/async"
)

func main() {
	result := async.All(
		func() error {
			time.Sleep(100 * time.Millisecond)
			return nil
		},
		func() error {
			time.Sleep(200 * time.Millisecond)
			return nil
		},
	)

	select {
	case err := <-result.Error:
		fmt.Printf("Got an error: %v\n", err)
	case <-result.Done:
		fmt.Printf("Success\n")
	}
}

func SpawnWithResult

func SpawnWithResult(N int, fn func(id int) error) Result

SpawnWithResult starts N functions concurrently

func (Result) Wait

func (r Result) Wait() []error

func (Result) WaitError

func (r Result) WaitError() error

Jump to

Keyboard shortcuts

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