pools

package
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: May 15, 2023 License: MIT Imports: 3 Imported by: 2

Documentation

Overview

Example
package main

import (
	"fmt"
	"time"

	"github.com/samber/lo"
	"github.com/xuender/kit/pools"
)

func main() {
	pool := pools.New(10, func(value, num int) string {
		time.Sleep(time.Millisecond)

		return fmt.Sprintf("%d: %d*2=%d", num, value, value*2)
	})

	outputs := pool.Post(lo.Range(100))

	fmt.Println(len(outputs))

}
Output:
100

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

type Pool[I, O any] struct {
	// contains filtered or unexported fields
}

Pool Goroutine 池.

Example (Context)
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/samber/lo"
	"github.com/xuender/kit/pools"
)

func main() {
	pool := pools.New(10, func(input lo.Tuple2[context.Context, int], num int) int {
		time.Sleep(time.Millisecond)

		return input.B * input.B
	})

	inputs := lo.Map(lo.Range(100), func(num, _ int) lo.Tuple2[context.Context, int] {
		return lo.T2(context.Background(), num)
	})
	outputs := pool.Post(inputs)

	fmt.Println(len(outputs))

}
Output:
100
Example (Error)
package main

import (
	"errors"
	"fmt"
	"time"

	"github.com/samber/lo"
	"github.com/xuender/kit/pools"
)

func main() {
	pool := pools.New(10, func(value, num int) lo.Tuple2[int, error] {
		time.Sleep(time.Millisecond)

		if value == 0 {
			// nolint
			return lo.T2(0, errors.New("divide by zero"))
		}

		return lo.T2[int, error](100/value, nil)
	})

	outputs := pool.Post(lo.Range(100))

	fmt.Println(len(outputs))

}
Output:
100

func New

func New[I, O any](size int, yield func(I, int) O) *Pool[I, O]

New 新建 Goroutine 池.

func (Pool) Post

func (p Pool) Post(inputs []I) []O

Jump to

Keyboard shortcuts

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