go-zero/core/threading/workergroup.go

25 lines
509 B
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package threading
// A WorkerGroup is used to run given number of workers to process jobs.
2020-07-26 17:09:05 +08:00
type WorkerGroup struct {
job func()
workers int
}
// NewWorkerGroup returns a WorkerGroup with given job and workers.
2020-07-26 17:09:05 +08:00
func NewWorkerGroup(job func(), workers int) WorkerGroup {
return WorkerGroup{
job: job,
workers: workers,
}
}
// Start starts a WorkerGroup.
2020-07-26 17:09:05 +08:00
func (wg WorkerGroup) Start() {
group := NewRoutineGroup()
for i := 0; i < wg.workers; i++ {
group.RunSafe(wg.job)
}
group.Wait()
}