golang 并行执行任务的例子

出自 kubernetes/staging/src/k8s.io/client-go/util/workqueue/parallelizer.go

type DoWorkPieceFunc func(piece int)

// Parallelize is a very simple framework that allow for parallelizing

// N independent pieces of work.

func Parallelize(workers, pieces int, doWorkPiece DoWorkPieceFunc) {

    toProcess := make(chan int, pieces)

    for i := 0; i < pieces; i++ {

        toProcess <- i

    }

    close(toProcess)

    if pieces < workers {

        workers = pieces

    }

    wg := sync.WaitGroup{}

    wg.Add(workers)

    for i := 0; i < workers; i++ {

        go func() {

            defer utilruntime.HandleCrash()

            defer wg.Done()

            for piece := range toProcess {

                doWorkPiece(piece)

            }

        }()

    }

    wg.Wait()

}

你可能感兴趣的:(golang 并行执行任务的例子)