mirror of https://github.com/panjf2000/ants.git
doc: add MultiPool & MultiPoolFunc example code and update READMEs. (#311)
This commit is contained in:
parent
1dbe4629aa
commit
8b0eb06e60
33
README.md
33
README.md
|
@ -122,6 +122,39 @@ func main() {
|
|||
wg.Wait()
|
||||
fmt.Printf("running goroutines: %d\n", p.Running())
|
||||
fmt.Printf("finish all tasks, result is %d\n", sum)
|
||||
if sum != 499500 {
|
||||
panic("the final result is wrong!!!")
|
||||
}
|
||||
|
||||
// Use the MultiPool and set the capacity of the 10 goroutine pools to unlimited.
|
||||
// If you use -1 as the pool size parameter, the size will be unlimited.
|
||||
// There are two load-balancing algorithms for pools: ants.RoundRobin and ants.LeastTasks.
|
||||
mp, _ := ants.NewMultiPool(10, -1, ants.RoundRobin)
|
||||
defer mp.ReleaseTimeout(5 * time.Second)
|
||||
for i := 0; i < runTimes; i++ {
|
||||
wg.Add(1)
|
||||
_ = mp.Submit(syncCalculateSum)
|
||||
}
|
||||
wg.Wait()
|
||||
fmt.Printf("running goroutines: %d\n", mp.Running())
|
||||
fmt.Printf("finish all tasks.\n")
|
||||
|
||||
// Use the MultiPoolFunc and set the capacity of 10 goroutine pools to (runTimes/10).
|
||||
mpf, _ := ants.NewMultiPoolWithFunc(10, runTimes/10, func(i interface{}) {
|
||||
myFunc(i)
|
||||
wg.Done()
|
||||
}, ants.LeastTasks)
|
||||
defer mpf.ReleaseTimeout(5 * time.Second)
|
||||
for i := 0; i < runTimes; i++ {
|
||||
wg.Add(1)
|
||||
_ = mpf.Invoke(int32(i))
|
||||
}
|
||||
wg.Wait()
|
||||
fmt.Printf("running goroutines: %d\n", mpf.Running())
|
||||
fmt.Printf("finish all tasks, result is %d\n", sum)
|
||||
if sum != 499500*2 {
|
||||
panic("the final result is wrong!!!")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
|
33
README_ZH.md
33
README_ZH.md
|
@ -122,6 +122,39 @@ func main() {
|
|||
wg.Wait()
|
||||
fmt.Printf("running goroutines: %d\n", p.Running())
|
||||
fmt.Printf("finish all tasks, result is %d\n", sum)
|
||||
if sum != 499500 {
|
||||
panic("the final result is wrong!!!")
|
||||
}
|
||||
|
||||
// Use the MultiPool and set the capacity of the 10 goroutine pools to unlimited.
|
||||
// If you use -1 as the pool size parameter, the size will be unlimited.
|
||||
// There are two load-balancing algorithms for pools: ants.RoundRobin and ants.LeastTasks.
|
||||
mp, _ := ants.NewMultiPool(10, -1, ants.RoundRobin)
|
||||
defer mp.ReleaseTimeout(5 * time.Second)
|
||||
for i := 0; i < runTimes; i++ {
|
||||
wg.Add(1)
|
||||
_ = mp.Submit(syncCalculateSum)
|
||||
}
|
||||
wg.Wait()
|
||||
fmt.Printf("running goroutines: %d\n", mp.Running())
|
||||
fmt.Printf("finish all tasks.\n")
|
||||
|
||||
// Use the MultiPoolFunc and set the capacity of 10 goroutine pools to (runTimes/10).
|
||||
mpf, _ := ants.NewMultiPoolWithFunc(10, runTimes/10, func(i interface{}) {
|
||||
myFunc(i)
|
||||
wg.Done()
|
||||
}, ants.LeastTasks)
|
||||
defer mpf.ReleaseTimeout(5 * time.Second)
|
||||
for i := 0; i < runTimes; i++ {
|
||||
wg.Add(1)
|
||||
_ = mpf.Invoke(int32(i))
|
||||
}
|
||||
wg.Wait()
|
||||
fmt.Printf("running goroutines: %d\n", mpf.Running())
|
||||
fmt.Printf("finish all tasks, result is %d\n", sum)
|
||||
if sum != 499500*2 {
|
||||
panic("the final result is wrong!!!")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -63,7 +63,7 @@ func main() {
|
|||
fmt.Printf("running goroutines: %d\n", ants.Running())
|
||||
fmt.Printf("finish all tasks.\n")
|
||||
|
||||
// Use the pool with a method,
|
||||
// Use the pool with a function,
|
||||
// set 10 to the capacity of goroutine pool and 1 second for expired duration.
|
||||
p, _ := ants.NewPoolWithFunc(10, func(i interface{}) {
|
||||
myFunc(i)
|
||||
|
@ -81,4 +81,34 @@ func main() {
|
|||
if sum != 499500 {
|
||||
panic("the final result is wrong!!!")
|
||||
}
|
||||
|
||||
// Use the MultiPool and set the capacity of the 10 goroutine pools to unlimited.
|
||||
// If you use -1 as the pool size parameter, the size will be unlimited.
|
||||
// There are two load-balancing algorithms for pools: ants.RoundRobin and ants.LeastTasks.
|
||||
mp, _ := ants.NewMultiPool(10, -1, ants.RoundRobin)
|
||||
defer mp.ReleaseTimeout(5 * time.Second)
|
||||
for i := 0; i < runTimes; i++ {
|
||||
wg.Add(1)
|
||||
_ = mp.Submit(syncCalculateSum)
|
||||
}
|
||||
wg.Wait()
|
||||
fmt.Printf("running goroutines: %d\n", mp.Running())
|
||||
fmt.Printf("finish all tasks.\n")
|
||||
|
||||
// Use the MultiPoolFunc and set the capacity of 10 goroutine pools to (runTimes/10).
|
||||
mpf, _ := ants.NewMultiPoolWithFunc(10, runTimes/10, func(i interface{}) {
|
||||
myFunc(i)
|
||||
wg.Done()
|
||||
}, ants.LeastTasks)
|
||||
defer mpf.ReleaseTimeout(5 * time.Second)
|
||||
for i := 0; i < runTimes; i++ {
|
||||
wg.Add(1)
|
||||
_ = mpf.Invoke(int32(i))
|
||||
}
|
||||
wg.Wait()
|
||||
fmt.Printf("running goroutines: %d\n", mpf.Running())
|
||||
fmt.Printf("finish all tasks, result is %d\n", sum)
|
||||
if sum != 499500*2 {
|
||||
panic("the final result is wrong!!!")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue