gpt4 book ai didi

go - 我如何发出多个请求并使用 goroutines 获得响应

转载 作者:数据小太阳 更新时间:2023-10-29 03:22:33 26 4
gpt4 key购买 nike

我需要在 url 中发出多个请求并获得返回并保存在返回的一部分中以便稍后使用,但它不起作用我的代码:

func main() {
requestTestGoRoutine()
log.Println("END")
}

func requestTestGoRoutine() {
done := make(chan *http.Response)
defer close(done)
for _, r := range requests {
go execute(r, done)
var p protocol
resp := <-done
json.NewDecoder(resp.Body).Decode(&p)
fmt.Println("protocol:", p)
protocols = append(protocols, p)

}
fmt.Println("protocols:", protocols)
}

func execute(r map[string]interface{}, done chan *http.Response) {
bodyRequest := new(bytes.Buffer)
json.NewEncoder(bodyRequest).Encode(r)
log.Println("Fazendo request...")
resp, err := requestControlTower(url, bodyRequest)
if err != nil {
log.Fatal(err)
}
done <- resp
}

我在终端的输出:

2018/06/29 16:10:26 Fazendo request...
protocol: {123456 Aprovado}
2018/06/29 16:10:38 Fazendo request...
protocol: {123457 Aprovado}
2018/06/29 16:10:48 Fazendo request...
protocol: {123458 Aprovado}
2018/06/29 16:10:58 Fazendo request...
protocol: {123459 Aprovado}
2018/06/29 16:11:08 Fazendo request...
protocol: {123410 Aprovado}
2018/06/29 16:11:18 Fazendo request...
protocol: {123411 Aprovado}
protocols: [{123456 Aprovado} {123457 Aprovado} {123458 Aprovado} {123459
Aprovado} {123410 Aprovado} {123411 Aprovado}]
2018/06/29 16:11:29 END

谁能帮帮我?

最佳答案

您的代码一次只处理一个请求的原因是您在请求循环中等待来自您的 channel 的响应:

resp := <-done

我发现使用 WaitGroup 和互斥量比使用 channel 容易得多,所以我在示例中使用了它们:

var protocolsMutex sync.Mutex
var wg sync.WaitGroup

func main() {
requestTestGoRoutine()
log.Println("END")
}

func requestTestGoRoutine() {
for _, r := range requests {
wg.Add(1)
go execute(r)
}

wg.Wait()

fmt.Println("protocols:", protocols)
}

func execute(r map[string]interface{}, done chan *http.Response) {
defer wg.Done()

bodyRequest := new(bytes.Buffer)
json.NewEncoder(bodyRequest).Encode(r)
resp, _ := requestControlTower(url, bodyRequest)

var p protocol
json.NewDecoder(resp.Body).Decode(&p)

protocolsMutex.Lock()
log.Println("Fazendo request...")
protocols = append(protocols, p)
protocolsMutex.Unlock()
}

此处,对于 requestTestGoRoutine() 中请求循环中的每个请求,我增加了 sync.WaitGroup一个开始一个execute请求的goroutine。在execute函数,我运行 defer wg.Done() ,一旦 goroutine 返回,它将使 waitgroup 递减 1。

在请求循环之后,我运行 wg.Wait() ,它将等到所有 goroutines 都调用了 wg.Done()在打印 protocols 之前 slice 。

A sync.Mutex用于控制对 protocols 的访问slice 以便一次只有一个 goroutine 可以访问它。如果 mutex 被锁定,其他 goroutine 将等待直到它被解锁,然后再继续下一段代码。如果没有互斥量,多个 goroutine 可以一次写入 slice ,从而导致竞争条件。我也移动了log.Println声明到此互斥体中以防止多个 goroutine 一次记录日志,这可能会导致日志中的行困惑。

我无权访问您的所有代码,因此未经测试。如果某些东西不起作用,请随时在评论中告诉我。

关于go - 我如何发出多个请求并使用 goroutines 获得响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51108149/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com