batch

package
v0.0.0-...-d31700d Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 6 Imported by: 0

README

batch

Run a sequence of data by batch, with optional concurrency, and optional progress printing.

Example

func ExampleBatches_example() {
	err := batches.Batches{
		From:      1,
		To:        307,
		BatchSize: 100,
		Concurrency: 1,
		Work: func(from, to int64) error {
			fmt.Printf("\t*do work for [%3d ~ %3d]*\t", from, to)
			return nil
		},
		Output: os.Stdout,
	}.Run()
	if err != nil {
		fmt.Println(err)
	}

	// Output:
	// 17:49:07 [1 ~ 307] total 4 batches
	// 17:49:07 [  1 ~ 100] ... 	*do work for [  1 ~ 100]*	 finished in  31.6µs
	// 17:49:07 [101 ~ 200] ... 	*do work for [101 ~ 200]*	 finished in   8.9µs
	// 17:49:07 [201 ~ 300] ... 	*do work for [201 ~ 300]*	 finished in   7.5µs
	// 17:49:07 [301 ~ 307] ... 	*do work for [301 ~ 307]*	 finished in   7.3µs
	// 17:49:07 [1 ~ 307] total 4 batches, finished in 518.5µs
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ElapsedTime

func ElapsedTime(start time.Time) string

func PrettyDuration

func PrettyDuration(d time.Duration) string
Example
fmt.Printf("%#v\n", PrettyDuration(67*time.Minute))
fmt.Printf("%#v\n", PrettyDuration(67*time.Second))
Output:

" 1h7m0s"
"   1m7s"

Types

type Batches

type Batches struct {
	From        int64
	To          int64
	BatchSize   uint32 // BatchSize defaults to 1 if is 0.
	Concurrency uint16
	Work        func(int64, int64) error
	Output      io.Writer
}

func (Batches) Run

func (b Batches) Run() (err error)
Example
err := Batches{
	From:      1,
	To:        307,
	BatchSize: 100,
	Work: func(from, to int64) error {
		fmt.Println(from, to)
		return nil
	},
	Output: os.Stderr,
}.Run()
if err != nil {
	fmt.Println(err)
}
Output:

1 100
101 200
201 300
301 307
Example (Concurrently)
var result = [][2]int64{}
var lock sync.Mutex
err := Batches{
	From:        307,
	To:          7,
	BatchSize:   100,
	Concurrency: 5,
	Work: func(from, to int64) error {
		time.Sleep(time.Millisecond)
		lock.Lock()
		defer lock.Unlock()
		result = append(result, [2]int64{from, to})
		return nil
	},
	Output: os.Stderr,
}.Run()
if err != nil {
	fmt.Println(err)
	return
}
sort.Slice(result, func(i, j int) bool {
	return result[i][0] > result[j][0]
})
fmt.Println(result)
Output:

[[307 208] [207 108] [107 8] [7 7]]
Example (ConcurrentlyWithError)
err := Batches{
	From:        1,
	To:          7,
	Concurrency: 2,
	Work: func(from, to int64) error {
		if from == 4 {
			return errors.New("error")
		}
		return nil
	},
	Output: os.Stderr,
}.Run()
if err != nil {
	fmt.Println(err)
}
Output:

error
Example (ConcurrentlyWithError2)
err := Batches{
	From:        1,
	To:          7,
	Concurrency: 2,
	Work: func(from, to int64) error {
		if from == 4 {
			return errors.New("error")
		}
		time.Sleep(10 * time.Millisecond)
		return nil
	},
	Output: os.Stderr,
}.Run()
if err != nil {
	fmt.Println(err)
}
Output:

error
Example (Desc)
err := Batches{
	From:      307,
	To:        7,
	BatchSize: 100,
	Work: func(from, to int64) error {
		fmt.Println(from, to)
		return nil
	},
	Output: os.Stderr,
}.Run()
if err != nil {
	fmt.Println(err)
}
Output:

307 208
207 108
107 8
7 7
Example (OneNumber)
err := Batches{
	From: 3,
	To:   3,
	Work: func(from, to int64) error {
		time.Sleep(time.Millisecond)
		fmt.Println(from, to)
		return nil
	},
	Output: os.Stderr,
}.Run()
if err != nil {
	fmt.Println(err)
}
Output:

3 3
Example (OneNumber2)
err := Batches{
	From:      3,
	To:        3,
	BatchSize: 3,
	Work: func(from, to int64) error {
		fmt.Println(from, to)
		return nil
	},
}.Run()
if err != nil {
	fmt.Println(err)
}
Output:

3 3
Example (SingleBatch)
err := Batches{
	From:      3,
	To:        1,
	BatchSize: 100,
	Work: func(from, to int64) error {
		time.Sleep(time.Second)
		fmt.Println(from, to)
		return nil
	},
	Output: os.Stderr,
}.Run()
if err != nil {
	fmt.Println(err)
}
Output:

3 1
Example (SingleStep)
err := Batches{
	From: 3,
	To:   1,
	Work: func(from, to int64) error {
		fmt.Println(from, to)
		return nil
	},
	Output: os.Stderr,
}.Run()
if err != nil {
	fmt.Println(err)
}
Output:

3 3
2 2
1 1
Example (WithError)
err := Batches{
	From:      1,
	To:        307,
	BatchSize: 100,
	Work: func(from, to int64) error {
		fmt.Println(from, to)
		if from == 201 {
			return errors.New("error")
		}
		return nil
	},
	Output: os.Stderr,
}.Run()
if err != nil {
	fmt.Println(err)
}
Output:

1 100
101 200
201 300
error

Jump to

Keyboard shortcuts

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