forked from mirror/ants
update example and readme
This commit is contained in:
parent
4b806f461b
commit
dbc39db648
49
README.md
49
README.md
|
@ -35,33 +35,66 @@ glide get github.com/panjf2000/ants
|
||||||
If your program will generate a massive number of goroutines and you don't want them to consume a vast amount of memory, with ants, all you need to do is to import ants package and submit all your tasks to the default limited pool created when ants was imported:
|
If your program will generate a massive number of goroutines and you don't want them to consume a vast amount of memory, with ants, all you need to do is to import ants package and submit all your tasks to the default limited pool created when ants was imported:
|
||||||
|
|
||||||
``` go
|
``` go
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/panjf2000/ants"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
|
"github.com/panjf2000/ants"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func myFunc() {
|
var sum int32
|
||||||
|
|
||||||
|
func myFunc(i interface{}) error {
|
||||||
|
n := i.(int)
|
||||||
|
atomic.AddInt32(&sum, int32(n))
|
||||||
|
fmt.Printf("run with %d\n", n)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func demoFunc() error {
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
fmt.Println("Hello World!")
|
fmt.Println("Hello World!")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
runTimes := 10000
|
runTimes := 1000
|
||||||
|
|
||||||
|
// use the common pool
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
// submit all your tasks to ants pool
|
|
||||||
for i := 0; i < runTimes; i++ {
|
for i := 0; i < runTimes; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
ants.Push(func() {
|
ants.Submit(func() error {
|
||||||
myFunc()
|
demoFunc()
|
||||||
wg.Done()
|
wg.Done()
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
fmt.Println("finish all tasks!")
|
fmt.Printf("running goroutines: %d\n", ants.Running())
|
||||||
}
|
fmt.Printf("finish all tasks.\n")
|
||||||
|
|
||||||
|
// use the pool with a function
|
||||||
|
// set 10 the size of goroutine pool
|
||||||
|
p, _ := ants.NewPoolWithFunc(10, func(i interface{}) error {
|
||||||
|
myFunc(i)
|
||||||
|
wg.Done()
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
// submit tasks
|
||||||
|
for i := 0; i < runTimes; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
p.Serve(i)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
fmt.Printf("running goroutines: %d\n", p.Running())
|
||||||
|
fmt.Printf("finish all tasks, result is %d\n", sum)
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Submit tasks
|
## Submit tasks
|
||||||
|
|
|
@ -28,6 +28,7 @@ import (
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/panjf2000/ants"
|
"github.com/panjf2000/ants"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var sum int32
|
var sum int32
|
||||||
|
@ -39,43 +40,42 @@ func myFunc(i interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// func main() {
|
func demoFunc() error {
|
||||||
// runTimes := 10000
|
time.Sleep(10 * time.Millisecond)
|
||||||
// var wg sync.WaitGroup
|
fmt.Println("Hello World!")
|
||||||
// // submit all your tasks to ants pool
|
return nil
|
||||||
// for i := 0; i < runTimes; i++ {
|
}
|
||||||
// wg.Add(1)
|
|
||||||
// ants.Submit(func() {
|
|
||||||
// myFunc()
|
|
||||||
// wg.Done()
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
// wg.Wait()
|
|
||||||
// fmt.Println("finish all tasks!")
|
|
||||||
// }
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
runTimes := 1000
|
runTimes := 1000
|
||||||
|
|
||||||
// set 100 the size of goroutine pool
|
// use the common pool
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
for i := 0; i < runTimes; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
ants.Submit(func() error {
|
||||||
|
demoFunc()
|
||||||
|
wg.Done()
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
fmt.Printf("running goroutines: %d\n", ants.Running())
|
||||||
|
fmt.Printf("finish all tasks.\n")
|
||||||
|
|
||||||
|
// use the pool with a function
|
||||||
|
// set 10 the size of goroutine pool
|
||||||
p, _ := ants.NewPoolWithFunc(10, func(i interface{}) error {
|
p, _ := ants.NewPoolWithFunc(10, func(i interface{}) error {
|
||||||
myFunc(i)
|
myFunc(i)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
// submit
|
// submit tasks
|
||||||
for i := 0; i < runTimes; i++ {
|
for i := 0; i < runTimes; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
p.Serve(i)
|
p.Serve(i)
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
//var m int
|
|
||||||
//var i int
|
|
||||||
//for n := range sum {
|
|
||||||
// m += n
|
|
||||||
//}
|
|
||||||
fmt.Printf("running goroutines: %d\n", p.Running())
|
fmt.Printf("running goroutines: %d\n", p.Running())
|
||||||
fmt.Printf("finish all tasks, result is %d\n", sum)
|
fmt.Printf("finish all tasks, result is %d\n", sum)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue