Skip to content

A goroutine pool - allow you limit number of goroutines, especially in cpu heavy load

License

Notifications You must be signed in to change notification settings

simpleapples/cyclone

Repository files navigation

Cyclone

goreportcard for simpleapples/cyclone License: MIT

Cyclone is a go package to spawn, reuse and manage a number of goroutines.

Install

Go Get:

go get github.com/simpleapples/cyclone

Dep:

dep ensure -add github.com/simpleapples/cyclone

Examples

size := runtime.NumCPU()
pool := NewWithClosure(int64(size), func(payload interface{}) interface{} {
    intV := payload.(int)
    fmt.Println(intV)
    return intV
})
defer pool.Close()

for i := 0; i < size; i++ {
    _, err := pool.Run(i)
}

Parallel jobs:

size := 5
total := 20

wg := sync.WaitGroup{}

pool := NewWithCallback(int64(size), func(payload interface{}) interface{} {
    intV := payload.(int)
    time.Sleep(1 * time.Second)
    return intV
}, func(result interface{}) {
    intV := result.(int)
    fmt.Println(intV)
    wg.Done()
})

for i := 0; i < total; i++ {
    wg.Add(1)
    pool.Run(i)
}
wg.Wait()

Change pool size:

pool.SetSize(100)
pool.SetSize(10000)

About

A goroutine pool - allow you limit number of goroutines, especially in cpu heavy load

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages