2 回答
TA贡献1842条经验 获得超12个赞
这是一个工作示例。当通道关闭时,范围退出,因此您可以在循环后处理任何剩余的项目。
package main
import (
"fmt"
"sync"
)
type Audit struct {
ID int
}
func upsertBigQueryAudits(audits []Audit) {
fmt.Printf("Processing batch of %d\n", len(audits))
for _, a := range audits {
fmt.Printf("%d ", a.ID)
}
fmt.Println()
}
func processAudits(audits <-chan Audit, batchSize int) {
var batch []Audit
for audit := range audits {
batch = append(batch, audit)
if len(batch) == batchSize {
upsertBigQueryAudits(batch)
batch = []Audit{}
}
}
if len(batch) > 0 {
upsertBigQueryAudits(batch)
}
}
func produceAudits(x int, to chan Audit) {
for i := 0; i < x; i++ {
to <- Audit{
ID: i,
}
}
}
const batchSize = 10
func main() {
var wg sync.WaitGroup
audits := make(chan Audit)
wg.Add(1)
go func() {
defer wg.Done()
processAudits(audits, batchSize)
}()
wg.Add(1)
go func() {
defer wg.Done()
produceAudits(25, audits)
close(audits)
}()
wg.Wait()
fmt.Println("Complete")
}
输出:
Processing batch of 10
0 1 2 3 4 5 6 7 8 9
Processing batch of 10
10 11 12 13 14 15 16 17 18 19
Processing batch of 5
20 21 22 23 24
Complete
TA贡献1820条经验 获得超10个赞
您也可以使用定时器。在这里玩例子https://play.golang.org/p/0atlGVCL-px
func printItems(items []int) {
fmt.Println(items)
return
}
func main() {
items := []int {1,2,3,4,5,6,7,8}
ch := make(chan int, 5)
go func(ch <-chan int) {
timer := time.NewTimer(1 * time.Second)
temp := make([]int, 0, 5)
for {
select {
case i := <- ch:
timer.Reset(1 * time.Second)
temp = append(temp, i)
if len(temp) == 5 {
printItems(temp)
temp = []int {}
}
case <- timer.C:
printItems(temp)
temp = []int {}
}
}
}(ch)
for k, i := range items {
fmt.Println("Send ", i)
ch <- i
if k == 7 {
time.Sleep(5 * time.Second)
}
}
}
- 2 回答
- 0 关注
- 120 浏览
添加回答
举报