gpt4 book ai didi

go - Go中限制并发的简单方法

转载 作者:IT王子 更新时间:2023-10-29 01:18:18 27 4
gpt4 key购买 nike

我有一个 CSV 文件,其中包含我需要通过 HTTP 获取的约 10k 个 URL。将 Go 例程的并发限制为一次不超过 16 个的最简单方法是什么?

func getUrl(url string) {
request := gorequest.New()
resp, body, errs := request.Get(each[1]).End()
_ = resp
_ = body
_ = errs
}

func main() {

csvfile, err := os.Open("urls.csv")
defer csvfile.Close()
reader := csv.NewReader(csvfile)
reader.FieldsPerRecord = -1
rawCSVdata, err := reader.ReadAll()

completed := 0
for _, each := range rawCSVdata {
go getUrl(each[1])
completed++
}
}

最佳答案

生产者-消费者模式:

package main

import (
"encoding/csv"
"os"
"sync"

"github.com/parnurzeal/gorequest"
)

const workersCount = 16

func getUrlWorker(urlChan chan string) {
for url := range urlChan {
request := gorequest.New()
resp, body, errs := request.Get(url).End()
_ = resp
_ = body
_ = errs
}
}

func main() {
csvfile, err := os.Open("urls.csv")
if err != nil {
panic(err)
}
defer csvfile.Close()

reader := csv.NewReader(csvfile)
reader.FieldsPerRecord = -1
rawCSVdata, err := reader.ReadAll()

var wg sync.WaitGroup
urlChan := make(chan string)

wg.Add(workersCount)

for i := 0; i < workersCount; i++ {
go func() {
getUrlWorker(urlChan)
wg.Done()
}()
}

completed := 0
for _, each := range rawCSVdata {
urlChan <- each[1]
completed++
}
close(urlChan)

wg.Wait()
}

关于go - Go中限制并发的简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33460672/

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