parallels

package module
v0.0.0-...-2cc2e8d Latest Latest
Warning

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

Go to latest
Published: May 28, 2019 License: MIT Imports: 4 Imported by: 0

README

go-parallels

GoDoc

Go package for parallel execute function.

How to use

Parallel execute all
parallels.Do(func(i int) error {
  fmt.Println(i)
  return nil
}, 10)
Specify number of concurrent executions
err := parallels.Do(func(i int) error {
  fmt.Println(i)
  return nil
}, 10, parallels.Concurrent(2))
if err != nil {
  // handling error
}

License

MIT

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

func Do(fn func(i int) error, n int, opts ...Option) error

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

func Concurrent(num int) Option

Concurrent returns an Option to specify the number of concurrent executions.

func WithContext

func WithContext(ctx context.Context) (Option, context.Context)

WithContext returns a Option for using Context and an associated Context derived from ctx.

type Panic

type Panic struct {
	Value interface{}
}

func (*Panic) Error

func (p *Panic) Error() string

Jump to

Keyboard shortcuts

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