Skip to content

fantasy9830/go-workerpool

Repository files navigation

go-workerpool

Installation

go get github.com/fantasy9830/go-workerpool

Usage

func WithContext

func WithContext(parent context.Context)

func WithMaxWorker

func WithMaxWorker(maxWorkers int)

func WithMaxTask

func WithMaxTask(maxTasks int)

func WithTimeout

func WithTimeout(timeout time.Duration)

examples

options := []workerpool.OptionFunc{
    workerpool.WithMaxWorker(5),
    workerpool.WithMaxTask(10),
}

pool := workerpool.New(options...)
defer pool.Stop()

var wg sync.WaitGroup
for i := 1; i <= 10; i++ {
    num := i
    wg.Add(1)
    pool.AddTask(func(ctx context.Context) {
        defer wg.Done()
        log.Println(num)
        time.Sleep(5 * time.Second)
    })
}

wg.Wait()

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages