forked from mirror/ants
Auto stash before merge of "develop" and "origin/develop"
This commit is contained in:
parent
aaa7bad491
commit
5326374a22
6
ants.go
6
ants.go
|
@ -39,9 +39,9 @@ const (
|
|||
// Init a instance pool when importing ants
|
||||
var defaultPool, _ = NewPool(DefaultPoolSize)
|
||||
|
||||
// Push submit a task to pool
|
||||
func Push(task f) error {
|
||||
return defaultPool.Push(task)
|
||||
// Submit submit a task to pool
|
||||
func Submit(task f) error {
|
||||
return defaultPool.Submit(task)
|
||||
}
|
||||
|
||||
// Running returns the number of the currently running goroutines
|
||||
|
|
|
@ -44,8 +44,9 @@ const (
|
|||
const RunTimes = 10000000
|
||||
const loop = 10
|
||||
|
||||
func demoFunc() {
|
||||
func demoFunc() error {
|
||||
time.Sleep(loop * time.Millisecond)
|
||||
return nil
|
||||
}
|
||||
|
||||
func demoPoolFunc(args interface{}) error {
|
||||
|
@ -101,10 +102,9 @@ func BenchmarkGoroutine(b *testing.B) {
|
|||
}
|
||||
|
||||
func BenchmarkAntsPool(b *testing.B) {
|
||||
b.N = 3
|
||||
for i := 0; i < b.N; i++ {
|
||||
for j := 0; j < RunTimes; j++ {
|
||||
ants.Push(demoFunc)
|
||||
ants.Submit(demoFunc)
|
||||
}
|
||||
b.Logf("running goroutines: %d", ants.Running())
|
||||
}
|
||||
|
|
|
@ -36,9 +36,10 @@ func TestDefaultPool(t *testing.T) {
|
|||
var wg sync.WaitGroup
|
||||
for i := 0; i < n; i++ {
|
||||
wg.Add(1)
|
||||
ants.Push(func() {
|
||||
ants.Submit(func() error {
|
||||
demoFunc()
|
||||
wg.Done()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
|
@ -111,7 +112,7 @@ func TestNoPool(t *testing.T) {
|
|||
// var wg sync.WaitGroup
|
||||
// for i := 0; i < n; i++ {
|
||||
// wg.Add(1)
|
||||
// p.Push(func() {
|
||||
// p.Submit(func() {
|
||||
// demoFunc()
|
||||
// //demoFunc()
|
||||
// wg.Done()
|
||||
|
|
|
@ -45,7 +45,7 @@ func myFunc(i interface{}) error {
|
|||
// // submit all your tasks to ants pool
|
||||
// for i := 0; i < runTimes; i++ {
|
||||
// wg.Add(1)
|
||||
// ants.Push(func() {
|
||||
// ants.Submit(func() {
|
||||
// myFunc()
|
||||
// wg.Done()
|
||||
// })
|
||||
|
|
12
pool.go
12
pool.go
|
@ -30,9 +30,9 @@ import (
|
|||
|
||||
type sig struct{}
|
||||
|
||||
type f func()
|
||||
type f func() error
|
||||
|
||||
// Pool accept the tasks from client,it will limit the total
|
||||
// Pool accept the tasks from client,it limits the total
|
||||
// of goroutines to a given number by recycling goroutines.
|
||||
type Pool struct {
|
||||
// capacity of the pool.
|
||||
|
@ -48,12 +48,10 @@ type Pool struct {
|
|||
// workers is a slice that store the available workers.
|
||||
workers []*Worker
|
||||
|
||||
// workerPool is a pool that saves a set of temporary objects.
|
||||
workerPool sync.Pool
|
||||
|
||||
// release is used to notice the pool to closed itself.
|
||||
release chan sig
|
||||
|
||||
// lock for synchronous operation
|
||||
lock sync.Mutex
|
||||
|
||||
once sync.Once
|
||||
|
@ -75,8 +73,8 @@ func NewPool(size int) (*Pool, error) {
|
|||
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
// Push submit a task to pool
|
||||
func (p *Pool) Push(task f) error {
|
||||
// Submit submit a task to pool
|
||||
func (p *Pool) Submit(task f) error {
|
||||
if len(p.release) > 0 {
|
||||
return ErrPoolClosed
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ import (
|
|||
|
||||
type pf func(interface{}) error
|
||||
|
||||
// PoolWithFunc accept the tasks from client,it will limit the total
|
||||
// PoolWithFunc accept the tasks from client,it limits the total
|
||||
// of goroutines to a given number by recycling goroutines.
|
||||
type PoolWithFunc struct {
|
||||
// capacity of the pool.
|
||||
|
@ -46,14 +46,13 @@ type PoolWithFunc struct {
|
|||
// workers is a slice that store the available workers.
|
||||
workers []*WorkerWithFunc
|
||||
|
||||
// workerPool is a pool that saves a set of temporary objects.
|
||||
//workerPool sync.Pool
|
||||
|
||||
// release is used to notice the pool to closed itself.
|
||||
release chan sig
|
||||
|
||||
// lock for synchronous operation
|
||||
lock sync.Mutex
|
||||
|
||||
// pf is the function for processing tasks
|
||||
poolFunc pf
|
||||
|
||||
once sync.Once
|
||||
|
|
Loading…
Reference in New Issue