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

标签 go

我需要在 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/

相关文章:

windows - Golang - 在 Windows 上更改构建工作路径

xml - Go:一次仅解码一个 XML 节点

go - Go 是否有任何 SFTP 或 SSH 库?

mongodb - Golang 和 Mgo 按 $natural : -1 排序

go - 如何使用标准库类型

Go Fyne 项目无法从 Linux 交叉编译到 Windows

amazon-web-services - 禁止使用aws sdk,但aws cli工作正常

go - 这段代码什么时候涵盖了 slice 元素?

html - 使用 goquery 从网站检索文本

go - CGo不在同一目录中编译C文件