Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "time" "github.com/xuender/kit/los" "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) }) defer pool.Close() outputs := pool.Post(los.Range[int](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 Native ¶ added in v1.0.46
type Native[I, O any] struct { // contains filtered or unexported fields }
Native 原生的协程控制.
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/los" "github.com/xuender/kit/pools" ) func main() { pool := pools.New(10, func(input lo.Tuple2[context.Context, int], _ int) int { time.Sleep(time.Millisecond) return input.B * input.B }) defer pool.Close() inputs := los.Map(los.Range[int](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/los" "github.com/xuender/kit/pools" ) func main() { pool := pools.New(10, func(value, _ 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) }) defer pool.Close() outputs := pool.Post(los.Range[int](100)) fmt.Println(len(outputs)) }
Output: 100
func (*Pool[I, O]) Run ¶ added in v1.0.19
func (p *Pool[I, O]) Run(input I) O
Run 执行单个任务.
Example ¶
package main import ( "fmt" "time" "github.com/xuender/kit/pools" ) func main() { pool := pools.New(10, func(value, _ int) string { time.Sleep(time.Millisecond) return fmt.Sprintf("%d*2=%d", value, value*2) }) defer pool.Close() fmt.Println(pool.Run(3)) }
Output: 3*2=6
type Simple ¶ added in v1.0.19
type Simple[T any] struct { // contains filtered or unexported fields }
Simle 简单协程池.
Example ¶
ExampleSimple is an example function.
package main import ( "fmt" "time" "github.com/xuender/kit/los" "github.com/xuender/kit/pools" ) func main() { pool := pools.NewSimple(2, func(_, _ int) { fmt.Println("a") }) defer pool.Close() pool.Post(los.Range[int](3)...) time.Sleep(time.Millisecond) }
Output: a a a
func (*Simple[T]) Wait ¶ added in v1.0.23
func (p *Simple[T]) Wait()
Wait 等待完毕.
Example ¶
package main import ( "fmt" "time" "github.com/xuender/kit/pools" ) func main() { pool := pools.NewSimple(2, func(_, _ int) { fmt.Println("a") }) defer pool.Close() pool.Post(1) pool.Post(2, 3) pool.Wait() time.Sleep(time.Millisecond) }
Output: a a a
type SyncPool ¶ added in v1.0.25
func NewSyncPool ¶ added in v1.0.25
Example ¶
ExampleNewSyncPool is an example function.
package main import ( "fmt" "github.com/xuender/kit/pools" ) type data struct { data int } // ExampleNewSyncPool is an example function. func main() { pool := pools.NewSyncPool(func() *data { return &data{1} }, func(d *data) { d.data = 1 }) d1 := pool.Get() d1.data = 3 pool.Put(d1) d2 := pool.Get() fmt.Println(d2.data) }
Output: 1
Click to show internal directories.
Click to hide internal directories.