mirror of
https://github.com/panjf2000/ants.git
synced 2025-12-17 02:21:04 +00:00
optimization
This commit is contained in:
parent
6581f1821d
commit
ab6390f6d0
@ -25,15 +25,17 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/panjf2000/ants"
|
"github.com/panjf2000/ants"
|
||||||
)
|
)
|
||||||
|
|
||||||
var str = "Hello World!"
|
var sum int32
|
||||||
|
|
||||||
func myFunc(i interface{}) error {
|
func myFunc(i interface{}) error {
|
||||||
s := i.(string)
|
n := i.(int)
|
||||||
fmt.Println(s)
|
atomic.AddInt32(&sum, int32(n))
|
||||||
|
fmt.Printf("run with %d\n", n)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,9 +68,14 @@ func main() {
|
|||||||
// submit
|
// submit
|
||||||
for i := 0; i < runTimes; i++ {
|
for i := 0; i < runTimes; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
p.Serve(str)
|
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.Println("finish all tasks!")
|
fmt.Printf("finish all tasks, result is %d\n", sum)
|
||||||
}
|
}
|
||||||
|
|||||||
22
pool.go
22
pool.go
@ -151,17 +151,21 @@ func (p *Pool) getWorker() *Worker {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
} else if w == nil {
|
} else if w == nil {
|
||||||
wp := p.workerPool.Get()
|
//wp := p.workerPool.Get()
|
||||||
if wp == nil {
|
//if wp == nil {
|
||||||
w = &Worker{
|
// w = &Worker{
|
||||||
pool: p,
|
// pool: p,
|
||||||
task: make(chan f, workerArgsCap),
|
// task: make(chan f, workerArgsCap),
|
||||||
}
|
// }
|
||||||
} else {
|
//} else {
|
||||||
w = wp.(*Worker)
|
// w = wp.(*Worker)
|
||||||
|
//}
|
||||||
|
w = &Worker{
|
||||||
|
pool: p,
|
||||||
|
task: make(chan f, workerArgsCap),
|
||||||
}
|
}
|
||||||
w.run()
|
w.run()
|
||||||
p.workerPool.Put(w)
|
//p.workerPool.Put(w)
|
||||||
}
|
}
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|||||||
30
pool_func.go
30
pool_func.go
@ -47,7 +47,7 @@ type PoolWithFunc struct {
|
|||||||
workers []*WorkerWithFunc
|
workers []*WorkerWithFunc
|
||||||
|
|
||||||
// workerPool is a pool that saves a set of temporary objects.
|
// workerPool is a pool that saves a set of temporary objects.
|
||||||
workerPool sync.Pool
|
//workerPool sync.Pool
|
||||||
|
|
||||||
// release is used to notice the pool to closed itself.
|
// release is used to notice the pool to closed itself.
|
||||||
release chan sig
|
release chan sig
|
||||||
@ -57,7 +57,6 @@ type PoolWithFunc struct {
|
|||||||
poolFunc pf
|
poolFunc pf
|
||||||
|
|
||||||
once sync.Once
|
once sync.Once
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPoolWithFunc generates a instance of ants pool with a specific function.
|
// NewPoolWithFunc generates a instance of ants pool with a specific function.
|
||||||
@ -108,7 +107,7 @@ func (p *PoolWithFunc) Cap() int {
|
|||||||
// Release Closed this pool
|
// Release Closed this pool
|
||||||
func (p *PoolWithFunc) Release() error {
|
func (p *PoolWithFunc) Release() error {
|
||||||
p.once.Do(func() {
|
p.once.Do(func() {
|
||||||
p.release<- sig{}
|
p.release <- sig{}
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -131,6 +130,8 @@ func (p *PoolWithFunc) getWorker() *WorkerWithFunc {
|
|||||||
if n < 0 {
|
if n < 0 {
|
||||||
if p.running >= p.capacity {
|
if p.running >= p.capacity {
|
||||||
waiting = true
|
waiting = true
|
||||||
|
} else {
|
||||||
|
p.running++
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
w = workers[n]
|
w = workers[n]
|
||||||
@ -156,23 +157,28 @@ func (p *PoolWithFunc) getWorker() *WorkerWithFunc {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
} else if w == nil {
|
} else if w == nil {
|
||||||
wp := p.workerPool.Get()
|
//wp := p.workerPool.Get()
|
||||||
if wp == nil {
|
//if wp == nil {
|
||||||
w = &WorkerWithFunc{
|
// w = &WorkerWithFunc{
|
||||||
pool: p,
|
// pool: p,
|
||||||
args: make(chan interface{}, workerArgsCap),
|
// args: make(chan interface{}, workerArgsCap),
|
||||||
}
|
// }
|
||||||
} else {
|
//} else {
|
||||||
w = wp.(*WorkerWithFunc)
|
// w = wp.(*WorkerWithFunc)
|
||||||
|
//}
|
||||||
|
w = &WorkerWithFunc{
|
||||||
|
pool: p,
|
||||||
|
args: make(chan interface{}),
|
||||||
}
|
}
|
||||||
w.run()
|
w.run()
|
||||||
p.workerPool.Put(w)
|
//p.workerPool.Put(w)
|
||||||
}
|
}
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
// putWorker puts a worker back into free pool, recycling the goroutines.
|
// putWorker puts a worker back into free pool, recycling the goroutines.
|
||||||
func (p *PoolWithFunc) putWorker(worker *WorkerWithFunc) {
|
func (p *PoolWithFunc) putWorker(worker *WorkerWithFunc) {
|
||||||
|
//p.workerPool.Put(worker)
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
p.workers = append(p.workers, worker)
|
p.workers = append(p.workers, worker)
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
|
|||||||
@ -40,7 +40,7 @@ type WorkerWithFunc struct {
|
|||||||
// run will start a goroutine to repeat the process
|
// run will start a goroutine to repeat the process
|
||||||
// that perform the function calls.
|
// that perform the function calls.
|
||||||
func (w *WorkerWithFunc) run() {
|
func (w *WorkerWithFunc) run() {
|
||||||
atomic.AddInt32(&w.pool.running, 1)
|
//atomic.AddInt32(&w.pool.running, 1)
|
||||||
go func() {
|
go func() {
|
||||||
for args := range w.args {
|
for args := range w.args {
|
||||||
if args == nil || len(w.pool.release) > 0 {
|
if args == nil || len(w.pool.release) > 0 {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user