Golang协程提前终止

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

Golang routine terminates early

问题

我刚开始使用Go语言并编写了我的第一个程序,但输出结果与预期不符。我编写了一个异步例程addUrl,它将URL添加到通道中5000次,而consumeUrl则从通道中移除并打印URL。

这个例程只运行了9次。为什么会这样呢?以下是代码和输出结果:

package main

import "fmt"
import "time"

var urlCount = 0

func main(){

    urlHolder := make(chan string,5000)

    fmt.Printf("Starting program")

    go addUrls(urlHolder)

    time.Sleep(time.Millisecond * 100)
    go consumeUrls(urlHolder)

    fmt.Printf("Done")

}

func addUrls(urlHolder chan string){
    var myurl string = "https://example.com/"

    for i:=0; i<5000 ; i++ {
        urlHolder<-myurl
        fmt.Printf(" %d url added \n",i)
        time.Sleep(time.Millisecond * 10)
    }
    
}

func consumeUrls(urlHolder chan string) {
    urlCount++
    urlsConsumed := <- urlHolder
    fmt.Printf("Pulled url %d",urlCount," ",urlsConsumed,"\n")
    time.Sleep(time.Millisecond * 20)
}

输出结果为:

Starting program
 0 url added 
 1 url added 
 2 url added 
 3 url added 
 4 url added 
 5 url added 
 6 url added 
 7 url added 
 8 url added 
Done

为什么循环执行了5000次,但程序在第8次时就终止了呢?

英文:

I just started using go and wrote my first program but the output is not as expected. I have writtern a async routine addUrl which adds url to channel 5000 times and consumeUrl removes from the channel and prints it.
The routine runs only 9 time. Why is it? Below is the code and output

package main

import &quot;fmt&quot;
import &quot;time&quot;

var urlCount = 0

func main(){

	urlHolder := make(chan string,5000)

	fmt.Printf(&quot;Starting program&quot;)

	go addUrls(urlHolder)

	time.Sleep(time.Millisecond * 100)
	go consumeUrls(urlHolder)

	fmt.Printf(&quot;Done&quot;)

}

func addUrls(urlHolder chan string){
	var myurl string = &quot;https://example.com/&quot;

	for i:=0; i&lt;5000 ; i++ {
		urlHolder&lt;-myurl
		fmt.Printf(&quot; %d url added \n&quot;,i)
		time.Sleep(time.Millisecond * 10)
	}
	
}

func consumeUrls(urlHolder chan string) {
	urlCount++
	urlsConsumed := &lt;- urlHolder
	fmt.Printf(&quot;Pulled url %d&quot;,urlCount,&quot; &quot;,urlsConsumed,&quot;\n&quot;)
	time.Sleep(time.Millisecond * 20)
}  

The output is

Starting program
 0 url added 
 1 url added 
 2 url added 
 3 url added 
 4 url added 
 5 url added 
 6 url added 
 7 url added 
 8 url added 
Done

Why is it terminating at 8 when loop is 5000?

答案1

得分: 1

你正在使用time.Sleep来等待main函数完成,但你应该使用WaitGroups。

这样你就不必尝试选择一些任意的时间,并希望它足够让你的程序完成,也不必担心设置太多的时间,导致你的程序闲置不做任何事情。

我在你的代码中添加了WaitGroups的实现,你可以在这里查看:

https://play.golang.org/p/1zn2JYefaA

此外,你的consumeUrls函数的编写方式不正确,它无法正确循环,并且你无法将所有内容打印到通道中。但由于这不是你具体的问题,我在这里不会解决这个问题。

英文:

You are using time.Sleep to wait for main to finish but you really should be using WaitGroups.

That way you don't have to try to pick some arbitrary time and hope it's enough for your program to finish, or worry about setting too much time and your program sits around doing nothing.

I've added the implementation of WaitGroups to your code here:

> https://play.golang.org/p/1zn2JYefaA

Also, the way your consumeUrls function is written is not looping properly and you won't get everything in your channel printed. But since that wasn't your specific question I won't address it here.

答案2

得分: -1

实际上,问题就像Nipun所说的那样,主程序提前终止了。

英文:

Actually The problem was like Nipun said, the main got terminated early.

huangapple
  • 本文由 发表于 2016年10月29日 11:12:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/40315686.html
匿名

发表评论

匿名网友

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

确定