golang异步批处理的例子

直接上代码,估计以后未必用,留着备忘吧

package main

import (
	"github.com/gogf/gf/container/glist"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"time"
)

func OnDemoRequest(r *ghttp.Request){
	maxCount := 100
	batchRequestChannel := make(chan int, maxCount)
	resultList := glist.New()

	startTime := time.Now()
	for i:=0; i< maxCount;i++{
		go doBatchRequest(i, resultList, batchRequestChannel)
	}
	<- batchRequestChannel

	endTime := time.Now()
	r.Response.Write("总请求时间 ", endTime.Sub(startTime))
}

func doBatchRequest(i int, resultList *glist.List, batchRequestChannel chan int ){
	content := g.Client().GetContent("http://127.0.0.1:5000")
	resultList.PushBack(content)
	batchRequestChannel <- i
}

func main() {
	server := g.Server()
	server.BindHandler("/demo", OnDemoRequest)
	server.SetPort(7654)
	server.Run()
}

golang异步批处理的例子

上一篇:使用Arthas动态修改日志级别


下一篇:CLR里的MethodTable,MethodDescChunk,MethodDesc,FixUpPreCode都是什么意思