Goroutine和Workgroup问题

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

Goroutine and Workgroup Issue

问题

我正在尝试使用Goroutines和Workgroups打印一组大型作业("1,2","3,4","5")与额外作业("one","two","three")的组合。

我期望得到以下输出(不一定按照这个顺序,但内部的Workgroups应该在外部的Workgroups之前完成):

正在进行大型作业:1,2
1_one
1_two
1_three
2_one
2_two
2_three
大型作业 1,2 完成!
正在进行大型作业:3,4
3_one
3_two
3_three
4_one
4_two
4_three
大型作业 3,4 完成!
正在进行大型作业:5
5_one
5_two
5_three
大型作业 5 完成!
所有大型作业完成!

然而,当我运行我的代码时,我得到了以下输出:

大型作业 1,2 完成!
大型作业 3,4 完成!
大型作业 5 完成!
正在进行大型作业:5
5_three
正在进行大型作业:1,2
1_three
正在进行大型作业:3,4
3_three
5_one
5_two
1_one
1_two
3_one
3_two
fatal error: all goroutines are asleep - deadlock!

如你所见,大型作业的第二部分(24)在goroutines中被“丢失”。

此外,All Big Jobs are done!消息从未被打印出来,因为所有的goroutines都陷入了沉睡状态,尽管每个Waitgroup应该已经完成(尽管缺少的24部分可能与此有关)。

我还注意到由于打印函数可能无法跟上Goroutines的速度,所以Done消息被首先打印出来。

这是我想要实现的非Goroutine版本:https://play.golang.org/p/zZpfyIbbn8

你有什么想法,我可能做错了什么?

英文:

I'm trying to print a set of Big Jobs ("1,2", "3,4", "5") in combination with Extra Jobs ("one", "two", "three") and using Goroutines and Workgroups to do so.

I am expecting the following output (not exactly in this order, but the internal Workgroups should finish first before the outer ones):

Big Job being done: 1,2
1_one
1_two
1_three
2_one
2_two
2_three
Big Job 1,2 is Done!
Big Job being done: 3,4
3_one
3_two
3_three
4_one
4_two
4_three
Big Job 3,4 is Done!
Big Job being done: 5
5_one
5_two
5_three
Big Job 5 is Done!
All Big Jobs are done!

Go Playground Link: https://play.golang.org/p/Hvbcmw06WY

However, when I run my code, I get the following output instead:

Big job  1,2  is Done!
Big job  3,4  is Done!
Big job  5  is Done!
Big Job being done: 5
5_three
Big Job being done: 1,2
1_three
Big Job being done: 3,4
3_three
5_one
5_two
1_one
1_two
3_one
3_two
fatal error: all goroutines are asleep - deadlock!

As you can see, the second portion of the Big Jobs (2 and 4) were somehow "lost" inside the goroutines.

Also, the All Big Jobs are done! message is never reached because all goroutines fall asleep somehow, even though each Waitgroup should have finished already (though the missing 2 and 4 portions might have something to do with it).

I also noticed that for some reason the Done messages are being printed first (although I'm assuming that the Print functions are simply having trouble catching up with the Goroutines).

Here is the non-Goroutine version of what I want to accomplish: https://play.golang.org/p/zZpfyIbbn8

Any idea what I might be doing wrong?

答案1

得分: 1

jobWG.Wait()应该放在for _, job := range jobs循环之外,而不是之内。
这是修复后的版本:https://play.golang.org/p/KNLS0y8xLg

英文:

jobWG.Wait() is inside the for _, job := range jobs loop when it should be outside.
Here is a fixed version https://play.golang.org/p/KNLS0y8xLg

huangapple
  • 本文由 发表于 2017年7月8日 20:42:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/44986340.html
匿名

发表评论

匿名网友

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

确定