英文:
Prevent the main() function from terminating before goroutines finish in Golang
问题
看一下这个人为的例子:
package main
import "fmt"
func printElo() {
fmt.Printf("Elo\n")
}
func printHello() {
fmt.Printf("Hello\n")
}
func main() {
fmt.Printf("This will print.")
i := 0
for i < 10 {
go printElo()
go printHello()
i++
}
}
这个程序的输出只会是"This will print"。printElo()
和printHello()
的goroutine的输出不会被打印出来,因为我猜测main()
函数的线程会在goroutine有机会开始执行之前就结束。
在Golang中,如何以惯用的方式使类似的代码正常工作而不会过早终止?
英文:
Have loook at this contrived example:
package main
import "fmt"
func printElo() {
fmt.Printf("Elo\n")
}
func printHello() {
fmt.Printf("Hello\n")
}
func main() {
fmt.Printf("This will print.")
i := 0
for i < 10 {
go printElo()
go printHello()
i++
}
}
The output of this program would be just "This will print". Output of goroutines printElo()
and printHello
will not be emitted because, I guess, the main()
function thread will finish before the goroutines have a chance to even start executing.
What is the idiomatic way to make similar code work in Golang and not terminate prematurely?
答案1
得分: 26
最简单、最清晰和可扩展的方法是使用sync.WaitGroup
:
var wg = &sync.WaitGroup{}
func printElo() {
defer wg.Done()
fmt.Printf("Elo\n")
}
func printHello() {
defer wg.Done()
fmt.Printf("Hello\n")
}
func main() {
fmt.Printf("This will print.")
i := 0
for i < 10 {
wg.Add(1)
go printElo()
wg.Add(1)
go printHello()
i++
}
wg.Wait()
}
输出结果(在Go Playground上尝试):
This will print.
Hello
Elo
Hello
Elo
Hello
Elo
Hello
Elo
Hello
Elo
Hello
Elo
Hello
Elo
Hello
Elo
Hello
Elo
Hello
使用sync.WaitGroup
时要遵循的简单规则:
- 在“原始”goroutine(启动新的goroutine之前)中调用
WaitGroup.Add()
- 建议使用延迟调用
WaitGroup.Done()
,这样即使goroutine发生恐慌,也会被调用 - 如果要将
WaitGroup
传递给其他函数(而不是使用包级别的变量),必须传递一个指针,否则WaitGroup
(一个结构体)将被复制,对副本调用Done()
方法不会影响原始的WaitGroup
。
英文:
Simplest, cleanest and "scalable" way to do it is to use a sync.WaitGroup
:
var wg = &sync.WaitGroup{}
func printElo() {
defer wg.Done()
fmt.Printf("Elo\n")
}
func printHello() {
defer wg.Done()
fmt.Printf("Hello\n")
}
func main() {
fmt.Printf("This will print.")
i := 0
for i < 10 {
wg.Add(1)
go printElo()
wg.Add(1)
go printHello()
i++
}
wg.Wait()
}
Output (try it on the Go Playground):
This will print.Hello
Elo
Hello
Elo
Hello
Elo
Hello
Elo
Hello
Elo
Hello
Elo
Hello
Elo
Hello
Elo
Hello
Elo
Hello
Elo
Simple "rules" to follow when doing it with sync.WaitGroup
:
-
call
WaitGroup.Add()
in the "original" goroutine (that starts a new) before thego
statement -
recommended to call
WaitGroup.Done()
deferred, so it gets called even if the goroutine panics -
if you want to pass
WaitGroup
to other functions (and not use a package level variable), you must pass a pointer to it, else theWaitGroup
(which is a struct) would be copied, and theDone()
method called on the copy wouldn't be observed on the original
答案2
得分: 3
如前所述,sync.WaitGroup
是在生产代码中的正确方式。但是在开发测试和调试目的时,您可以在 main()
的末尾添加 select{}
语句。
func main(){
go routine()
...
select{}
}
然后,main()
永远不会返回,您可以使用例如 Ctrl-C
来终止它。这不是惯用的做法,不会在生产环境中使用,但在开发过程中是一种非常快速简便的方法。
英文:
As already mentioned sync.WaitGroup
is a right way in production code. But when developing for test and debug purposes you can just add select{}
statement at the end or the main()
.
func main(){
go routine()
...
select{}
}
main()
then never returns and you would kill it with for example Ctrl-C
. It isn't idiomatic, never used in production, but just very quick easy hack when developing.
答案3
得分: 2
你可以使用同步包并查看waitgroups
。你可以查看我设置的一个工作中的Goplayground。
基本上
package main
import (
"fmt"
"sync"
)
//当工作完成时,接收到wg的引用并休眠
func printElo(wg *sync.WaitGroup) {
fmt.Printf("Elo\n")
defer wg.Done()
}
//当工作完成时,接收到wg的引用并休眠
func printHello(wg *sync.WaitGroup) {
fmt.Printf("Hello\n")
defer wg.Done()
}
func main() {
//创建一个新的WaitGroup
var wg sync.WaitGroup
fmt.Println("This will print.")
for i := 0; i < 10; i++ {
//向waitgroup添加一个新条目
wg.Add(1)
//新的Goroutine,接收到wg的引用
go printHello(&wg)
//向waitgroup添加一个新条目
wg.Add(1)
//新的Goroutine,接收到wg的引用
go printElo(&wg)
}
//等待直到所有工作完成
wg.Wait()
}
英文:
You can use sync package and take a look at waitgroups
. You can take a look at a working Goplayground I set up.
Essentially
package main
import (
"fmt"
"sync"
)
//Takes a reference to the wg and sleeps when work is done
func printElo(wg *sync.WaitGroup) {
fmt.Printf("Elo\n")
defer wg.Done()
}
//Takes a reference to the wg and sleeps when work is done
func printHello(wg *sync.WaitGroup) {
fmt.Printf("Hello\n")
defer wg.Done()
}
func main() {
//Create a new WaitGroup
var wg sync.WaitGroup
fmt.Println("This will print.")
for i := 0; i < 10; i++ {
//Add a new entry to the waitgroup
wg.Add(1)
//New Goroutine which takes a reference to the wg
go printHello(&wg)
//Add a new entry to the waitgroup
wg.Add(1)
//New Goroutine which takes a reference to the wg
go printElo(&wg)
}
//Wait until everything is done
wg.Wait()
}
答案4
得分: 1
如果你只是想玩一下结果,可以使用等待输入的“hack”方法:
package main
import (
"fmt"
"bufio"
"os"
)
func printElo() {
fmt.Printf("Elo\n")
}
func printHello() {
fmt.Printf("Hello\n")
}
func main() {
fmt.Printf("This will print.")
i := 0
for i < 10 {
go printElo()
go printHello()
i++
}
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
}
如果想学习如何进行同步,请阅读有关 sync 包的内容:
package main
import (
"fmt"
"sync"
)
var wg sync.WaitGroup
func printElo() {
fmt.Printf("Elo\n")
wg.Done()
}
func printHello() {
fmt.Printf("Hello\n")
wg.Done()
}
func main() {
fmt.Printf("This will print.")
i := 0
for i < 10 {
wg.Add(2)
go printElo()
go printHello()
i++
}
wg.Wait()
}
英文:
If you want just to play with results you can use "hack" with waiting for input:
package main
import (
"fmt"
"bufio"
"os"
)
func printElo() {
fmt.Printf("Elo\n")
}
func printHello() {
fmt.Printf("Hello\n")
}
func main() {
fmt.Printf("This will print.")
i := 0
for i < 10 {
go printElo()
go printHello()
i++
}
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
}
If want to learn how to do synchronization read about sync package:
package main
import (
"fmt"
"sync"
)
var wg sync.WaitGroup
func printElo() {
fmt.Printf("Elo\n")
wg.Done()
}
func printHello() {
fmt.Printf("Hello\n")
wg.Done()
}
func main() {
fmt.Printf("This will print.")
i := 0
for i < 10 {
wg.Add(2)
go printElo()
go printHello()
i++
}
wg.Wait()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论