温馨提示:本文翻译自stackoverflow.com,查看原文请点击:go - goroutines run post api in parallel with list of data

go - goroutines与数据列表并行运行post api

发布于 2020-03-27 16:05:20

这是我的第一次使用go的经验,我来自python背景,我正尝试使用具有不同发布数据集的goroutines并行运行此发布api(https://reqres.in/api/users)在此代码中是变量jsonStr。

任何与数据列表并行运行此api的帮助,将不胜感激

我的资料

data = [{“ name”:“ bonny gaud”,“ movies”:[“ Terminator”,“ Transformer”],{“ name”:“ Sarah palin”,“ movies”:[“没有古老的国家”,“占士邦”] }

我的代码:

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "time"
    "bytes"
)

func main() {
    start := time.Now()
    url := "https://reqres.in/api/users"
    fmt.Println("URL:>", url)
    var jsonStr = []byte(`{"name": "paul rudd", "movies": ["I Love You Man", "Role Models"] }`)
    req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
    req.Header.Set("X-Custom-Header", "myvalue")
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    fmt.Println("response Status:", resp.Status)
    fmt.Println("response Headers:", resp.Header)
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println("response Body:", string(body))

    fmt.Print("Everything:", time.Since(start))
    fmt.Print(string(body))

}

查看更多

查看更多

提问者
min2bro
被浏览
57
yogy fresta rahmawan 2020-01-31 17:17

您可以使用waitGroup或errGroup。这是示例:

package main

import (
    "bytes"
    "fmt"
    "golang.org/x/sync/errgroup"
    "io/ioutil"
    "net/http"
    "time"
)

func main() {
    dataSets := []string{`{"name": "paul rudd", "movies": ["I Love You Man", "Role Models"] }`,
        `{"name": "paul rudd", "movies": ["I Love You Man", "Role Models"] }`}

    var eg errgroup.Group

    client := http.DefaultClient
    for _, jsonStr := range dataSets {
        eg.Go(func() error {
            return callClient(client, jsonStr)
        })
    }

    if err := eg.Wait(); err != nil {
        fmt.Printf("Encountered error: %v", err)
    }

    fmt.Println("Successfully finished.")
}

func callClient(client *http.Client, jsonStr string) error {
    start := time.Now()
    url := "https://reqres.in/api/users"
    fmt.Println("URL:>", url)

    req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(jsonStr)))
    req.Header.Set("X-Custom-Header", "myvalue")
    req.Header.Set("Content-Type", "application/json")

    resp, err := client.Do(req)
    if err != nil {
        return fmt.Errorf("send request: %w", err)
    }
    defer resp.Body.Close()

    fmt.Println("response Status:", resp.Status)
    fmt.Println("response Headers:", resp.Header)

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return fmt.Errorf("read body: %w", err)
    }

    fmt.Println("response Body:", string(body))

    fmt.Print("Everything:", time.Since(start))
    fmt.Print(string(body))

    return nil
}

发布
问题

分享
好友

手机
浏览

扫码手机浏览