登录
首页 >  Golang >  Go问答

从通道中获取多个 Go 例程

来源:stackoverflow

时间:2024-02-23 14:27:27 300浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《从通道中获取多个 Go 例程》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。

问题内容

我有一个使用以下代码接收地图切片的通道:

func submitrecords(records []map[string]string) {
    batch := []map[string]string{}
    ch := make(chan []map[string]string)
    batchct := 1
    go func() {
        for _, v := range records {
            batch = append(batch, v)
            if len(batch) == 150 {
                ch <- batch
                batch = nil
            }
        }
        close(ch)
    }()
}

我提交这些记录的 api 接受最多 150 个批次。为了加快速度,我想启动 4 个 go 例程来同时处理通道中的记录。一旦记录进入通道,它们的处理顺序并不重要。

目前,我对上面单独运行的代码进行了以下更新来处理它:

func submitRecords(records []map[string]string) {
    batch := []map[string]string{}
    ch := make(chan []map[string]string)
    batchCt := 1
    go func() {
        for _, v := range records {
            batch = append(batch, v)
            if len(batch) == 150 {
                ch <- batch
                batch = nil
            }
        }
        close(ch)
    }()

    for b := range ch {
        str, _ := json.Marshal(b)
        fmt.Printf("Sending batch at line %d\n", (batchCt * 150))

        payload := strings.NewReader(string(str))
        client := &http.Client{}
        req, err := http.NewRequest(method, url, payload)
        if err != nil {
            fmt.Println(err)
        }

        login, _ := os.LookupEnv("Login")
        password, _ := os.LookupEnv("Password")
        req.Header.Add("user_name", login)
        req.Header.Add("password", password)
        req.Header.Add("Content-Type", "application/json")

        res, err := client.Do(req)
        if err != nil {
            fmt.Println(err)
        }
        batchCt++
    }
}

我该如何修改它,让 4 个 go 例程从通道中提取并发送这些请求?或者,这是否可能/我是否误解了 go 例程的功能?


解决方案


func process(ch chan []map[string]string) {
    for b := range ch {
        str, _ := json.marshal(b)

        // this wont work, or has to be included in the payload from the channel
        // fmt.printf("sending batch at line %d\n", (batchct * 150))

        payload := strings.newreader(string(str))
        client := &http.client{}
        req, err := http.newrequest(method, url, payload)
        if err != nil {
            fmt.println(err)
        }

        login, _ := os.lookupenv("login")
        password, _ := os.lookupenv("password")
        req.header.add("user_name", login)
        req.header.add("password", password)
        req.header.add("content-type", "application/json")

        res, err := client.do(req)
        if err != nil {
            fmt.println(err)
        }
        // batchct++
    }
    done <- 1
}

func submitrecords(records []map[string]string) {
    batch := []map[string]string{}
    ch := make(chan []map[string]string)
    
    go process(ch)
    go process(ch)
    go process(ch)
    go process(ch)

    // batchct := 1
    for _, v := range records {
        batch = append(batch, v)
        if len(batch) == 150 {
            ch <- batch
            batch = []map[string]string{}
        }
    }
    // send the last not to size batch
    ch <- batch
    close(ch)
}

使用 int 和 sleep https://play.golang.org/p/q5bUhXt9aUn 的演示示例

package main

import (
    "fmt"
    "time"
)

func process(processor int, ch chan []int, done chan int) {
    for batch := range ch {
        // Do something.. sleep or http requests will let the other workers work as well
        time.Sleep(time.Duration(len(batch)) * time.Millisecond)
        fmt.Println(processor, batch)
    }
    done <- 1
}

const batchSize = 3

func main() {
    records := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
    ch := make(chan []int)
    done := make(chan int)

    go process(1, ch, done)
    go process(2, ch, done)
    go process(3, ch, done)
    go process(4, ch, done)

    batch := make([]int, 0, batchSize)
    for _, v := range records {
        batch = append(batch, v)
        if len(batch) == batchSize {
            ch <- batch
            batch = make([]int, 0, batchSize)
        }
    }
    ch <- batch
    close(ch)

    <-done
    <-done
    <-done
    <-done
}

好了,本文到此结束,带大家了解了《从通道中获取多个 Go 例程》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>