-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoroutine.go
More file actions
42 lines (33 loc) · 724 Bytes
/
Copy pathgoroutine.go
File metadata and controls
42 lines (33 loc) · 724 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package u
import (
"github.com/panjf2000/ants/v2"
"sync"
)
// ConcurrentHandle 并发任务
func ConcurrentHandle(dataMapList []map[string]interface{}, callback func(map[string]interface{}) map[string]interface{}) {
ch := make(chan bool)
for _, item := range dataMapList {
go func(dataArg map[string]interface{}) {
callback(dataArg)
ch <- true
}(item)
}
Foreach(len(dataMapList), func() {
<- ch
})
}
// PoolGoroutine 携程池
func PoolGoroutine(poolNum int, countNum int, function func()) {
defer ants.Release()
var wg sync.WaitGroup
pool, _ := ants.NewPool(poolNum)
task := func() {
function()
wg.Done()
}
for i:=0;i<countNum;i++ {
wg.Add(1)
_ = pool.Submit(task)
}
wg.Wait()
}