Go Tour#10:爬虫解决方案中的done通道有什么用途?

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

Go Tour #10: What is the use of that done channel in the crawler solution

问题

在这个解决方案中,我对并发Go教程的第十个幻灯片有一个问题,涉及以下部分:

done := make(chan bool)
for i, u := range urls {
    fmt.Printf("-> Crawling child %v/%v of %v : %v.\n", i, len(urls), url, u)
    go func(url string) {
        Crawl(url, depth-1, fetcher)
        done <- true
    }(u)
}
for i, u := range urls {
    fmt.Printf("<- [%v] %v/%v Waiting for child %v.\n", url, i, len(urls), u)
    <-done
}
fmt.Printf("<- Done with %v\n", url)

done通道中添加和删除true的目的是什么?运行这两个单独的for循环有什么作用?这只是为了阻塞,直到Go协程完成吗?我知道这只是一个示例练习,但这不是在第一次创建新线程时失去了意义吗?

为什么不能只调用go Crawl(url, depth-1, fetcher),而不需要第二个for循环和done通道?这是因为所有变量共享内存空间的原因吗?

谢谢!

英文:

In this solution to the tenth slide of the concurrency Go tour I have a question regarding the following section:

done := make(chan bool)
for i, u := range urls {
	fmt.Printf(&quot;-&gt; Crawling child %v/%v of %v : %v.\n&quot;, i, len(urls), url, u)
	go func(url string) {
		Crawl(url, depth-1, fetcher)
		done &lt;- true
	}(u)
}
for i, u := range urls {
	fmt.Printf(&quot;&lt;- [%v] %v/%v Waiting for child %v.\n&quot;, url, i, len(urls), u)
	&lt;-done
}
fmt.Printf(&quot;&lt;- Done with %v\n&quot;, url)

What purpose does adding and removing true from the the channel done and running the two separate for loops have? Is it just to block until the go routine finishes? I know this is an example exercise, but doesn't that kind of defeat the point of spinning out a new thread in the first place?

Why can't you just call go Crawl(url, depth-1, fetcher) without the 2nd for loop and the done channel? Is it because of the shared memory space for all the variables?

Thanks!

答案1

得分: 3

第一个for循环调度多个goroutine运行,并在一个urls切片上进行迭代。

第二个循环在每个url上阻塞,等待其对应的Crawl()调用完成。所有的Crawl()函数将并行运行并完成它们的工作,并在主线程有机会在每个url上接收到done通道的消息之前阻塞退出。

在我看来,更好的实现方式是使用sync.WaitGroup。除非fetcher进行锁定,否则该代码可能记录错误的内容,这取决于每个Crawl()调用所花费的时间。

如果你想确保已完成Crawl()的url,可以将done通道的类型更改为string,并在Crawl()完成时发送url而不是true。然后,我们可以在第二个循环中接收到该url

示例:

done := make(chan string)
for _, u := range urls {
    fmt.Printf("-> Crawling %s\n", u)
    go func(url string) {
        Crawl(url, depth-1, fetcher)
        done <- url
    }(u)
}
for range urls {
    fmt.Printf("<- Waiting for next child\n")
    u := <-done
    fmt.Printf("  Done... %s\n", u)
}
英文:

The first for loop schedules multiple goroutines to run and is iterating over a slice of urls.

The second loop blocks on each url, waiting until its corresponding Crawl() invocation has completed. All the Crawl()ers will run and do their work in parallel and block exiting until the main thread has a chance to receive a message on the done channel for each url.

In my opinion, a better way to implement this is to use a sync.WaitGroup. This code could log the wrong thing depending on how long each Crawl() invocation takes unless fetcher locks.

If you want to be sure of the url that finished Crawl()ing, you could change the type of the done channel to string and send the url instead of true upon a Crawl() completion. Then, we could receive the url in the second loop.

Example:

done := make(chan string)
for _, u := range urls {
    fmt.Printf(&quot;-&gt; Crawling %s\n&quot;, u)
    go func(url string) {
        Crawl(url, depth-1, fetcher)
        done &lt;- url
    }(u)
}
for range urls {
    fmt.Printf(&quot;&lt;- Waiting for next child\n&quot;)
    u := &lt;-done
    fmt.Printf(&quot;  Done... %s\n&quot;, u)
}

huangapple
  • 本文由 发表于 2017年9月11日 11:49:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/46147914.html
匿名

发表评论

匿名网友

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

确定