我试图了解 channel 和协程,并尝试编写一个协程来向服务器发出并发 API 请求
但是当我使用 goroutine 运行代码时,它似乎花费了与没有 goroutine 相同的时间。
func sendUser(user string, ch chan<- string) {
resp,err := http.get("URL"/user)
//do the processing and get resp=string
ch <- resp
}
func AsyncHTTP(users []string) ([]string, error) {
ch := make(chan string)
var responses []string
var user string
for _ , user = range users {
go sendUser(user, ch)
for {
select {
case r := <-ch:
if r.err != nil {
fmt.Println(r.err)
}
responses = append(responses, r)
**//Is there a better way to show that the processing of response is complete**?
if len(responses) == len(users) {
return responses, nil
}
case <-time.After(50 * time.Millisecond):
fmt.Printf(".")
}
}
}
return responses, nil
}
问题:
即使我使用了 goroutine,请求完成时间与没有 goroutine 时一样吗?我对 goroutine 做错了什么吗?
为了告诉作业不要再在这里等待,我正在使用:
if len(responses) == len(users)
有没有更好的方法来表明response的处理完成,告诉ch不要再等待了?
什么是 wait.Syncgroup?我如何在我的 goroutine 中使用它?
最佳答案
我可能会做这样的事情..
func sendUser(user string, ch chan<- string, wg *sync.WaitGroup) {
defer wg.Done()
resp, err := http.Get("URL/" + user)
if err != nil {
log.Println("err handle it")
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("err handle it")
}
ch <- string(b)
}
func AsyncHTTP(users []string) ([]string, error) {
ch := make(chan string)
var responses []string
var user string
var wg sync.WaitGroup
for _, user = range users {
wg.Add(1)
go sendUser(user, ch, &wg)
}
// close the channel in the background
go func() {
wg.Wait()
close(ch)
}()
// read from channel as they come in until its closed
for res := range ch {
responses = append(responses, res)
}
return responses, nil
}
它允许在发送时从 channel 中读取。通过使用 WaitGroup ,我将知道何时关闭 channel 。通过将 waitgroup 和 close 放在一个 goroutine 中,我可以“实时”地从 channel 中读取而不会阻塞。
关于走例程:Making concurrent API requests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45337881/