Go例程未接收通过通道发送的所有数据–玩具示例程序

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

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 := &lt;-fChan:
      fmt.Printf(&quot;sending: %d down result chan\n&quot;, fn())
      result &lt;- fn()
    case &lt;-time.After(time.Second * 2):
      close(fChan)
    }
  }
}

func recieve(result chan int){
  for {
    select {
    case x := &lt;-result:
      fmt.Printf(&quot;recieved: %d from result chan\n&quot;, x)
    case &lt;-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(&quot;sending a function that returns: %d down function chan\n&quot;, fn())
    fChan &lt;- 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

这段代码存在几个问题:

  • 当主函数返回时,程序终止。它不会等待runreceive协程完成。
  • 关闭通道存在竞争条件。不能保证发送方在超时之前停止发送。
  • 如果主函数不退出,那么for { select { } }循环将一直打印零值。在关闭的通道上接收会返回零值。
英文:

There are a couple of issues with this code:

  • The program terminates when main returns. It does not wait for the run and receive 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.

huangapple
  • 本文由 发表于 2015年12月29日 08:50:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/34502933.html
匿名

发表评论

匿名网友

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

确定