From dbc39db64859f26b6912e7d07bedd14c1754b5fe Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sat, 26 May 2018 08:42:10 +0800 Subject: [PATCH 1/2] update example and readme --- README.md | 49 ++++++++++++++++++++++++++++++++++++++++-------- examples/main.go | 44 +++++++++++++++++++++---------------------- 2 files changed, 63 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 567d24b..7133b19 100644 --- a/README.md +++ b/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: ``` go + package main import ( "fmt" - "github.com/panjf2000/ants" "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!") + return nil } func main() { - runTimes := 10000 + runTimes := 1000 + + // use the common pool var wg sync.WaitGroup - // submit all your tasks to ants pool for i := 0; i < runTimes; i++ { wg.Add(1) - ants.Push(func() { - myFunc() + ants.Submit(func() error { + demoFunc() wg.Done() + return nil }) } 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 diff --git a/examples/main.go b/examples/main.go index 141029b..3d9e26d 100644 --- a/examples/main.go +++ b/examples/main.go @@ -28,6 +28,7 @@ import ( "sync/atomic" "github.com/panjf2000/ants" + "time" ) var sum int32 @@ -39,43 +40,42 @@ func myFunc(i interface{}) error { return nil } -// func main() { -// runTimes := 10000 -// var wg sync.WaitGroup -// // submit all your tasks to ants pool -// for i := 0; i < runTimes; i++ { -// wg.Add(1) -// ants.Submit(func() { -// myFunc() -// wg.Done() -// }) -// } -// wg.Wait() -// fmt.Println("finish all tasks!") -// } +func demoFunc() error { + time.Sleep(10 * time.Millisecond) + fmt.Println("Hello World!") + return nil +} func main() { runTimes := 1000 - // set 100 the size of goroutine pool - + // use the common pool 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 { myFunc(i) wg.Done() return nil }) - // submit + // submit tasks for i := 0; i < runTimes; i++ { wg.Add(1) p.Serve(i) } wg.Wait() - //var m int - //var i int - //for n := range sum { - // m += n - //} fmt.Printf("running goroutines: %d\n", p.Running()) fmt.Printf("finish all tasks, result is %d\n", sum) } From 2fef3ca1f7207f7b8b87f9e4de51a144419efa73 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sat, 26 May 2018 17:26:36 +0800 Subject: [PATCH 2/2] update - --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7133b19..a0d646b 100644 --- a/README.md +++ b/README.md @@ -98,9 +98,9 @@ func main() { ``` ## Submit tasks -Tasks can be submitted by calling `ants.Push(func())` +Tasks can be submitted by calling `ants.Submit(func())` ```go -ants.Push(func() {}) +ants.Submit(func() {}) ``` ## Custom limited pool @@ -110,7 +110,7 @@ Ants also supports custom limited pool. You can use the `NewPool` method to crea // set 10000 the size of goroutine pool p, _ := ants.NewPool(10000) // submit a task -p.Push(func() {}) +p.Submit(func() {}) ``` ## Readjusting pool capacity