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