有没有更好的方法来跟踪goroutine的响应?

huangapple go评论122阅读模式
英文:

Any better way to keep track of goroutine responses?

问题

我正在努力理解goroutines。我创建了一个简单的程序,可以在多个搜索引擎上并行执行相同的搜索。目前,为了跟踪响应的数量,我计算了我收到的响应数量。但这似乎有点业余。

在下面的代码中,有没有更好的方法来知道我是否收到了所有goroutines的响应?

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "log"
  6. )
  7. type Query struct {
  8. url string
  9. status string
  10. }
  11. func search(url string, out chan Query) {
  12. fmt.Printf("Fetching URL %s\n", url)
  13. resp, err := http.Get(url)
  14. if err != nil {
  15. log.Fatal(err)
  16. }
  17. defer resp.Body.Close()
  18. out <- Query{url, resp.Status}
  19. }
  20. func main() {
  21. searchTerm := "carrot"
  22. fmt.Println("Hello world! Searching for ", searchTerm)
  23. searchEngines := []string{
  24. "http://www.bing.co.uk/?q=",
  25. "http://www.google.co.uk/?q=",
  26. "http://www.yahoo.co.uk/?q="}
  27. out := make(chan Query)
  28. for i := 0; i < len(searchEngines); i++ {
  29. go search(searchEngines[i]+searchTerm, out)
  30. }
  31. progress := 0
  32. for {
  33. // 有没有更好的方法来执行这一步?
  34. if progress >= len(searchEngines) {
  35. break
  36. }
  37. fmt.Println("Polling...")
  38. query := <-out
  39. fmt.Printf("Status from %s was %s\n", query.url, query.status)
  40. progress++
  41. }
  42. }
英文:

I'm trying to get my head around goroutines. I've created a simple program that performs the same search in parallel across multiple search engines. At the moment to keep track of the number of responses, I count the number I've received. It seems a bit amateur though.

Is there a better way of knowing when I've received a response from all of the goroutines in the following code?

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;net/http&quot;
  5. &quot;log&quot;
  6. )
  7. type Query struct {
  8. url string
  9. status string
  10. }
  11. func search (url string, out chan Query) {
  12. fmt.Printf(&quot;Fetching URL %s\n&quot;, url)
  13. resp, err := http.Get(url)
  14. if err != nil {
  15. log.Fatal(err)
  16. }
  17. defer resp.Body.Close()
  18. out &lt;- Query{url, resp.Status}
  19. }
  20. func main() {
  21. searchTerm := &quot;carrot&quot;
  22. fmt.Println(&quot;Hello world! Searching for &quot;, searchTerm)
  23. searchEngines := []string{
  24. &quot;http://www.bing.co.uk/?q=&quot;,
  25. &quot;http://www.google.co.uk/?q=&quot;,
  26. &quot;http://www.yahoo.co.uk/?q=&quot;}
  27. out := make(chan Query)
  28. for i := 0; i &lt; len(searchEngines); i++ {
  29. go search(searchEngines[i] + searchTerm, out)
  30. }
  31. progress := 0
  32. for {
  33. // is there a better way of doing this step?
  34. if progress &gt;= len(searchEngines) {
  35. break
  36. }
  37. fmt.Println(&quot;Polling...&quot;)
  38. query := &lt;-out
  39. fmt.Printf(&quot;Status from %s was %s\n&quot;, query.url, query.status)
  40. progress++
  41. }
  42. }

答案1

得分: 12

请使用sync.WaitGroup,在pkg doc中有一个示例

  1. searchEngines := []string{
  2. "http://www.bing.co.uk/?q=",
  3. "http://www.google.co.uk/?q=",
  4. "http://www.yahoo.co.uk/?q="}
  5. var wg sync.WaitGroup
  6. out := make(chan Query)
  7. for i := 0; i < len(searchEngines); i++ {
  8. wg.Add(1)
  9. go func (url string) {
  10. defer wg.Done()
  11. fmt.Printf("Fetching URL %s\n", url)
  12. resp, err := http.Get(url)
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. defer resp.Body.Close()
  17. out <- Query{url, resp.Status}
  18. }(searchEngines[i] + searchTerm)
  19. }
  20. wg.Wait()
英文:

Please use sync.WaitGroup, there is an example in the pkg doc

  1. searchEngines := []string{
  2. &quot;http://www.bing.co.uk/?q=&quot;,
  3. &quot;http://www.google.co.uk/?q=&quot;,
  4. &quot;http://www.yahoo.co.uk/?q=&quot;}
  5. var wg sync.WaitGroup
  6. out := make(chan Query)
  7. for i := 0; i &lt; len(searchEngines); i++ {
  8. wg.Add(1)
  9. go func (url string) {
  10. defer wg.Done()
  11. fmt.Printf(&quot;Fetching URL %s\n&quot;, url)
  12. resp, err := http.Get(url)
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. defer resp.Body.Close()
  17. out &lt;- Query{url, resp.Status}
  18. }(searchEngines[i] + searchTerm)
  19. }
  20. wg.Wait()

huangapple
  • 本文由 发表于 2013年2月23日 17:00:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/15038834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定