Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BlockIter ¶
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)
}
}
Output:
func Iter ¶
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)
}
}
Output:
func Run ¶
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 ¶
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)
}
}
Output:
Types ¶
type Result ¶
type Result struct {
Done <-chan struct{}
Error <-chan error
}
func All ¶
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")
}
}
Output:
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)
}
}
Output:
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")
}
}
Output:
func SpawnWithResult ¶
SpawnWithResult starts N functions concurrently
Click to show internal directories.
Click to hide internal directories.