Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "time" parallels "github.com/kamiaka/go-parallels" ) func main() { ls := []string{"A", "B", "C", "D", "E", "F"} start := time.Now() parallels.Do(func(i int) error { time.Sleep(sec(i + 1)) fmt.Printf("#%v: end %v, %v\n", i, ls[i], sinceSec(start)) return nil }, len(ls)) } // sec converts and returns a given integer into seconds. // Note: tests emulates 1 / 10 seconds as 1 second. func sec(i int) time.Duration { return time.Duration(i) * 100 * time.Millisecond } // sinceSec returns the time elapsed since t. // Note: tests emulates 1 / 10 seconds as 1 second. func sinceSec(t time.Time) time.Duration { return time.Since(t) / sec(1) * sec(1) * 10 }
Output: #0: end A, 1s #1: end B, 2s #2: end C, 3s #3: end D, 4s #4: end E, 5s #5: end F, 6s
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
Do executes function in parallel n times.
Example (WithConcurrent) ¶
package main import ( "fmt" "time" parallels "github.com/kamiaka/go-parallels" ) func main() { ls := []string{"A", "B", "C", "D", "E", "F"} start := time.Now() parallels.Do(func(i int) error { time.Sleep(sec(i + 1)) fmt.Printf("#%v: end %v, %v\n", i, ls[i], sinceSec(start)) return nil }, len(ls), parallels.Concurrent(2)) } // sec converts and returns a given integer into seconds. // Note: tests emulates 1 / 10 seconds as 1 second. func sec(i int) time.Duration { return time.Duration(i) * 100 * time.Millisecond } // sinceSec returns the time elapsed since t. // Note: tests emulates 1 / 10 seconds as 1 second. func sinceSec(t time.Time) time.Duration { return time.Since(t) / sec(1) * sec(1) * 10 }
Output: #0: end A, 1s #1: end B, 2s #2: end C, 4s #3: end D, 6s #4: end E, 9s #5: end F, 12s
Example (WithContext) ¶
package main import ( "context" "fmt" "time" parallels "github.com/kamiaka/go-parallels" ) func main() { ls := []string{"A", "B", "C", "D", "E", "F"} ctx, cancel := context.WithCancel(context.Background()) ctxOpt, ctx := parallels.WithContext(ctx) start := time.Now() parallels.Do(func(i int) error { t := time.NewTimer(sec(i + 1)) defer t.Stop() select { case <-ctx.Done(): fmt.Printf("#%v: canceled %v, %v\n", i, ls[i], sinceSec(start)) case <-t.C: fmt.Printf("#%v: end %v, %v\n", i, ls[i], sinceSec(start)) if i == 2 { cancel() } } return nil }, len(ls), ctxOpt) } // sec converts and returns a given integer into seconds. // Note: tests emulates 1 / 10 seconds as 1 second. func sec(i int) time.Duration { return time.Duration(i) * 100 * time.Millisecond } // sinceSec returns the time elapsed since t. // Note: tests emulates 1 / 10 seconds as 1 second. func sinceSec(t time.Time) time.Duration { return time.Since(t) / sec(1) * sec(1) * 10 }
Output: #0: end A, 1s #1: end B, 2s #2: end C, 3s #5: canceled F, 3s #3: canceled D, 3s #4: canceled E, 3s
Example (WithError) ¶
package main import ( "fmt" "time" parallels "github.com/kamiaka/go-parallels" ) func main() { ls := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I"} start := time.Now() err := parallels.Do(func(i int) error { time.Sleep(sec(i + 1)) fmt.Printf("#%v: end %v, %v\n", i, ls[i], sinceSec(start)) if i >= 2 { return fmt.Errorf("#%v: an error occurred %v %v", i, ls[i], sinceSec(start)) } return nil }, len(ls), parallels.Concurrent(2)) if err != nil { fmt.Println(err) } } // sec converts and returns a given integer into seconds. // Note: tests emulates 1 / 10 seconds as 1 second. func sec(i int) time.Duration { return time.Duration(i) * 100 * time.Millisecond } // sinceSec returns the time elapsed since t. // Note: tests emulates 1 / 10 seconds as 1 second. func sinceSec(t time.Time) time.Duration { return time.Since(t) / sec(1) * sec(1) * 10 }
Output: #0: end A, 1s #1: end B, 2s #2: end C, 4s #3: end D, 6s #2: an error occurred C 4s
Example (WithPanic) ¶
package main import ( "fmt" parallels "github.com/kamiaka/go-parallels" ) func main() { err := parallels.Do(func(i int) error { if i == 4 { panic("It is bad luck to choose one of four things!") } return nil }, 10) if err != nil { fmt.Println(err) } }
Output: caught panic in goroutine: It is bad luck to choose one of four things!
Types ¶
type Option ¶
type Option func(*group)
Option is option to specify the bahavior of parallel execution.
func Concurrent ¶
Concurrent returns an Option to specify the number of concurrent executions.
Click to show internal directories.
Click to hide internal directories.