this is a simple lib to do work parallel.
for example: you need do some check, called check A
, check B
, check C
, and those check are not interdependent, so we can do those check at the same time
- make a
content
ctx := context.Background()
- make
worker
worker := parallelworker.NewParallelWorker(ctx)
- add all your
worker
worker.AddWorker(func(ctx context.Context) {
select {
case <-ctx.Done():
// if ctx is cancel, you should return your function
return
default:
}
// do something
})
-
wait for finish
this function will be blocked, so you need add a timeout context to control it, this context is different from the first one
timeOutCtx := context.WithTimeout(ctx, time.Second * 5)
worker.WaitContext(timeOutCtx)