繁体   English   中英

我该如何通过goroutines请求倍数并获得响应

[英]how can I make multiples request and get response with goroutines

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

有人可以帮助我吗?

您的代码一次只处理一个请求的原因是,您正在等待请求循环中通道的响应:

resp := <-done

我发现使用等待组和互斥锁比使用通道要容易得多,因此在示例中使用它们:

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加1,并为该请求启动execute goroutine。 execute函数中,我运行defer wg.Done() ,一旦goroutine返回,它将使waitgroup减一。

在请求循环之后,我运行wg.Wait() ,它将等待直到所有goroutine都调用了wg.Done()然后再打印protocols切片。

sync.Mutex用于控制对protocols切片的访问,以便一次只能有一个goroutine可以访问它。 如果互斥锁已锁定,则其他goroutine将等待直到其被解锁,然后再继续下一部分代码。 如果没有互斥锁,一个以上的goroutine可以一次写入该片,从而导致竞争状态。 我还将log.Println语句移到了该互斥锁中,以防止一次记录多个goroutine,这可能会导致日志中出现混乱的行。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM