英文:
Wait for the termination of n goroutines
问题
我需要启动大量的goroutine并等待它们终止。直观的方法似乎是使用一个通道来等待它们全部完成:
package main
type Object struct {
//数据
}
func (obj *Object) Update(channel chan int) {
//更新数据
channel <- 1
return
}
func main() {
channel := make(chan int, n)
list := make([]Object, n, m)
for {
for _, object := range list {
go object.Update(channel)
}
for i := 0; i < n; i++ {
<-channel
}
//现在所有的数据都已经更新完毕。重新开始
}
}
但问题是对象的数量以及goroutine的数量可能会改变。是否可以更改通道的缓冲区大小?
也许有更优雅的方法来做到这一点吗?
英文:
I need to start a huge amount of goroutines and wait for their termination. The intuitive way seems to use a channel to wait till all of them are finished :
package main
type Object struct {
//data
}
func (obj *Object) Update(channel chan int) {
//update data
channel <- 1
return
}
func main() {
channel := make(chan int, n)
list := make([]Object, n, m)
for {
for _, object := range list {
go object.Update(channel)
}
for i := 0; i < n; i++ {
<-channel
}
//now everything has been updated. start again
}
}
But the problem is that the amount of objects and therefore the amount of goroutines could change. Is it possible to change the buffer size of a channel ?
Is there maybe a more elegant way to do this ?
答案1
得分: 35
我已经使用WaitGroup作为解决这个问题的方法。翻译你当前的代码,并添加一些日志以清楚地显示发生了什么:
package main
import "sync"
import "fmt"
import "time"
type Object struct {
//数据
}
func (obj *Object) Update(wg *sync.WaitGroup) {
//更新数据
time.Sleep(time.Second)
fmt.Println("更新完成")
wg.Done()
return
}
func main() {
var wg sync.WaitGroup
list := make([]Object, 5)
for {
for _, object := range list {
wg.Add(1)
go object.Update(&wg)
}
//现在所有的数据都已经更新完毕。重新开始
wg.Wait()
fmt.Println("组完成")
}
}
英文:
I've used WaitGroup as a solution to this problem. Translating your current code, with some logs to make it clear what's happening:
package main
import "sync"
import "fmt"
import "time"
type Object struct {
//data
}
func (obj *Object) Update(wg *sync.WaitGroup) {
//update data
time.Sleep(time.Second)
fmt.Println("Update done")
wg.Done()
return
}
func main() {
var wg sync.WaitGroup
list := make([]Object, 5)
for {
for _, object := range list {
wg.Add(1)
go object.Update(&wg)
}
//now everything has been updated. start again
wg.Wait()
fmt.Println("Group done")
}
}
答案2
得分: 4
这个任务并不是非常简单,很容易写出有错误的代码。我建议使用标准库中的现成解决方案 - sync.WaitGroup
。引用自链接中的说明:
> WaitGroup用于等待一组goroutine完成。主goroutine调用Add来设置要等待的goroutine的数量。然后每个goroutine运行并在完成时调用Done。同时,可以使用Wait来阻塞,直到所有goroutine都完成。
英文:
This task in not exactly trivial, it's quite easy to write a buggy one. I recommend to use a ready made solution in the stdlib - sync.WaitGroup
. Quoting from the link:
> A WaitGroup waits for a collection of goroutines to finish. The main goroutine calls Add to set the number of goroutines to wait for. Then each of the goroutines runs and calls Done when finished. At the same time, Wait can be used to block until all goroutines have finished.
答案3
得分: 1
@tjameson做了一个很好的工作,解释了如何使用WaitGroup
,如何将对WaitGroup
对象的引用传递给函数。我对他的示例做出的一个改变是在你完成时利用defer
。我认为这个defer ws.Done()
应该是你的函数中的第一条语句。
我喜欢WaitGroup
的简单性。然而,我不喜欢我们需要传递对goroutine的引用,因为这意味着并发逻辑将与业务逻辑混合在一起。
所以我想出了这个通用函数来解决这个问题:
// Parallelize并行化函数调用
func Parallelize(functions ...func()) {
var waitGroup sync.WaitGroup
waitGroup.Add(len(functions))
defer waitGroup.Wait()
for _, function := range functions {
go func(copy func()) {
defer waitGroup.Done()
copy()
}(function)
}
}
所以你的示例可以这样解决:
type Object struct {
//数据
}
func (obj *Object) Update() {
//更新数据
time.Sleep(time.Second)
fmt.Println("更新完成")
return
}
func main() {
functions := []func(){}
list := make([]Object, 5)
for _, object := range list {
function := func(obj Object){ object.Update() }(object)
functions = append(functions, function)
}
Parallelize(functions...)
fmt.Println("组完成")
}
如果你想使用它,你可以在这里找到它 https://github.com/shomali11/util
英文:
@tjameson did a great job explaining how to use WaitGroup
, how to pass a reference to your WaitGroup
object to your function. The one change I would make to his example is leverage defer
when you are Done
. I think this defer ws.Done()
should be the first statement in your function.
I like WaitGroup
's simplicity. However, I do not like that we need to pass the reference to the goroutine because that would mean that the concurrency logic would be mixed with your business logic.
So I came up with this generic function to solve this problem for me:
// Parallelize parallelizes the function calls
func Parallelize(functions ...func()) {
var waitGroup sync.WaitGroup
waitGroup.Add(len(functions))
defer waitGroup.Wait()
for _, function := range functions {
go func(copy func()) {
defer waitGroup.Done()
copy()
}(function)
}
}
So your example could be solved this way:
type Object struct {
//data
}
func (obj *Object) Update() {
//update data
time.Sleep(time.Second)
fmt.Println("Update done")
return
}
func main() {
functions := []func(){}
list := make([]Object, 5)
for _, object := range list {
function := func(obj Object){ object.Update() }(object)
functions = append(functions, function)
}
Parallelize(functions...)
fmt.Println("Group done")
}
If you would like to use it, you can find it here https://github.com/shomali11/util
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论