英文:
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("-> 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)
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("-> 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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论