1 回答
TA贡献1890条经验 获得超9个赞
生产者-消费者模式:
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()
}
- 1 回答
- 0 关注
- 163 浏览
添加回答
举报