Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" wp "gitlab.com/mipimipi/go-utils/workerpool" ) func main() { // fibN is the result structure for Fibonacci calculation: Mapping of n to the // n-th Fibonacci number type fibN struct { n int fib int } // fibonacci calculates the n-th Fibonacci number var fibonacci func(int) fibN fibonacci = func(n int) fibN { if n < 2 { return fibN{n: n, fib: n} } return fibN{n: n, fib: fibonacci(n-1).fib + fibonacci(n-2).fib} } // create worker pool with 10 workers pl := wp.NewPool(10) // submit tasks to worker pool. To not have to wait, that's done in a Go // routine go func() { for n := 0; n < 100; n++ { pl.In <- wp.Task{ Name: "fibonacci", F: func(i interface{}) interface{} { return fibonacci(i.(int)) }, In: n} } close(pl.In) }() // retrieve results from worker pool for res := range pl.Out { fmt.Printf("%3d-th Fibonacci Number = %d\n", res.Out.(fibN).n, res.Out.(fibN).fib) } }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Pool ¶
type Pool struct { In chan Task // input channel Out chan Result // output channel // contains filtered or unexported fields }
Pool represents a worker pool. The pool mainly consists of channels that let it communicate with the outside (submitting tasks, receiving results etc.).
Click to show internal directories.
Click to hide internal directories.