You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
import (
"context""fmt""os""os/signal""time""github.com/dreamph/workerpool"
)
funcmain() {
ctx, stop:=signal.NotifyContext(context.Background(), os.Interrupt)
deferstop()
pool:=workerpool.NewPool(ctx, 20, 10)
// Submit 20 tasksfori:=0; i<20; i++ {
n:=ipool.Submit(func() {
TestFn(n)
})
}
// Stop the pool and wait for all submitted tasks to completepool.Wait()
// Output:/* End of task #0 End of task #10 End of task #5 End of task #13 End of task #8 End of task #4 End of task #9 End of task #11 End of task #12 End of task #19 End of task #7 End of task #15 End of task #16 End of task #17 End of task #6 End of task #18 End of task #3 End of task #1 End of task #14 End of task #2 */
}
funcTestFn(nint) {
time.Sleep(3*time.Second)
fmt.Println(fmt.Sprintf("End of task #%d", n))
}
Example Worker Pool with Result
package main
import (
"context""os""os/signal""time""fmt""github.com/dreamph/workerpool"
)
typeResultstruct {
Successbool`json:"success"`Datastring`json:"data"`
}
funcmain() {
ctx, stop:=signal.NotifyContext(context.Background(), os.Interrupt)
deferstop()
pool:=workerpool.NewResultPool[int, Result](ctx, 20, 10)
// Submit 20 tasksfori:=0; i<20; i++ {
n:=ipool.Submit(n, func() (Result, error) {
returnTestFn(n)
})
}
// Stop the pool and wait for all submitted tasks to completepoolResponse:=pool.Wait()
poolResults:=poolResponse.Result()
forkey, value:=rangepoolResults {
fmt.Println(fmt.Sprintf("%d : [%v,%s]", key, value.Data.Success, value.Data.Data))
}
// Output:/* 0 : [true,Result of task #0] 2 : [true,Result of task #2] 15 : [true,Result of task #15] 3 : [true,Result of task #3] 12 : [true,Result of task #12] 8 : [true,Result of task #8] 6 : [true,Result of task #6] 11 : [true,Result of task #11] 1 : [true,Result of task #1] 9 : [true,Result of task #9] 18 : [true,Result of task #18] 4 : [true,Result of task #4] 13 : [true,Result of task #13] 19 : [true,Result of task #19] 10 : [true,Result of task #10] 7 : [true,Result of task #7] 14 : [true,Result of task #14] 16 : [true,Result of task #16] 17 : [true,Result of task #17] 5 : [true,Result of task #5] */
}
funcTestFn(nint) (Result, error) {
v:=fmt.Sprintf("Result of task #%d", n)
//fmt.Println(v)time.Sleep(3*time.Second)
returnResult{Success: true, Data: v}, nil
}