英文:
Go routine not receiving all data sent through channel -- toy example program
问题
我只会为你提供翻译服务,以下是你提供的代码的翻译:
我只是在玩弄Go
,可以说是试驾一下。我遇到了一个问题,一个用于接收3
个整数的Go协程似乎只接收到了一个。
type simpleFunction func() int
func run(fChan chan simpleFunction, result chan int) {
for{
select {
case fn := <-fChan:
fmt.Printf("sending: %d down result chan\n", fn())
result <- fn()
case <-time.After(time.Second * 2):
close(fChan)
}
}
}
func recieve(result chan int){
for {
select {
case x := <-result:
fmt.Printf("recieved: %d from result chan\n", x)
case <-time.After(time.Second * 2):
close(result)
}
}
}
func main() {
fns := []simpleFunction{
func() int {return 1},
func() int {return 2},
func() int {return 3},
}
fChan := make(chan simpleFunction)
result := make(chan int)
go run(fChan, result)
go recieve(result)
for _, fn := range fns {
fmt.Printf("sending a function that returns: %d down function chan\n", fn())
fChan <- fn
}
}
输出结果如下:
sending a function that returns: 1 down function chan
sending: 1 down result chan
recieved: 1 from result chan
sending a function that returns: 2 down function chan
sending a function that returns: 3 down function chan
sending: 2 down result chan
sending: 3 down result chan
所以,你可以看到,第一个函数的一切似乎都很顺利,但之后就不太好了。有什么建议或提示吗?
英文:
I'm just playing around with Go
, taking it for a test drive so to speak. I'm having a problem where a go routine that is mean to receive 3
integers only seems to receive one.
type simpleFunction func() int
func run(fChan chan simpleFunction, result chan int) {
for{
select {
case fn := <-fChan:
fmt.Printf("sending: %d down result chan\n", fn())
result <- fn()
case <-time.After(time.Second * 2):
close(fChan)
}
}
}
func recieve(result chan int){
for {
select {
case x := <-result:
fmt.Printf("recieved: %d from result chan\n", x)
case <-time.After(time.Second * 2):
close(result)
}
}
}
So, as you can see the run
routine receives functions, evaluates them, and then sends the result down the result
channel.
Here's my main
/test
:
func main() {
fns := []simpleFunction{
func() int {return 1},
func() int {return 2},
func() int {return 3},
}
fChan := make(chan simpleFunction)
result := make(chan int)
go run(fChan, result)
go recieve(result)
for _, fn := range fns {
fmt.Printf("sending a function that returns: %d down function chan\n", fn())
fChan <- fn
}
}
And here's my output:
sending a function that returns: 1 down function chan
sending: 1 down result chan
recieved: 1 from result chan
sending a function that returns: 2 down function chan
sending a function that returns: 3 down function chan
sending: 2 down result chan
sending: 3 down result chan
So, as you can see, everything seems to go swimmingly for the first function, but it's not so hot afterwards. Any tips or suggestions?
答案1
得分: 2
这段代码存在几个问题:
- 当主函数返回时,程序终止。它不会等待
run
和receive
协程完成。 - 关闭通道存在竞争条件。不能保证发送方在超时之前停止发送。
- 如果主函数不退出,那么
for { select { } }
循环将一直打印零值。在关闭的通道上接收会返回零值。
英文:
There are a couple of issues with this code:
- The program terminates when main returns. It does not wait for the
run
andreceive
goroutines to complete. - There's a race on closing the channels. There's no guarantee that the sender will top sending before the timeout.
- If main does not exit, then the
for { select { } }
loops will spin forever printing zero values. Receive on a closed channel returns the zero value.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论