What is the preferred approach to do non-blocking select statement with channels in Go

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

What is the preferred approach to do non-blocking select statement with channels in Go

问题

这里有一个我想要做的例子:

func (zoo *Zoo) feedAnimals(food Food) {
    for animal := range zoo.Animals {
        select {
        case animal.EatChan() <- food:
        default: // 什么都不做
        }
    }
}

动物的EatChan有一个小缓冲区,有时feedAnimals的调用频率比某些动物消耗食物的速度更快。当这种情况发生时,如果我省略select块中的default语句,select语句将阻塞for循环,其他饥饿的动物将无法获得食物。因此,我宁愿跳过已经饱食的动物(即通道已达到其容量)。

然而,只有一个空的default语句对我来说感觉很奇怪。有没有更好的方法来做到这一点?

英文:

Here's an example with what I want to do

func (zoo *Zoo) feedAnimals(food Food) {
    for animal := range zoo.Animals {
        select {
        case animal.EatChan() &lt;- food:
        default: // Do nothing
        }
    }
}

Animal's EatChan has a small buffer, some times rate of which feedAnimals is called more often than the rate of which some animals can consume the food. When that happens, if I omit default statement in the select block, the select statement will block the for loop and other hungry animals can't get their food. So I'd rather skip the animal that is full (i.e. the channel has reached its capacity.)

However, having an empty default feels weird to me. Is there a better way to do this?

答案1

得分: 2

然而,对我来说,使用一个空的默认值感觉很奇怪。

不应该感觉奇怪。

有没有更好的方法来做这个?

没有。

你已经做得很正确了。空的默认值不是一个“什么都不做”的语句,而是一个“不阻塞”的语句。这只是非阻塞选择的语义。

英文:

> However, having an empty default feels weird to me.

It shouldn't.

> Is there a better way to do this?

No.

You're already doing it properly. The empty default isn't a "do nothing" statement, it's a "don't block" statement. This is just the semantics of how a non-blocking select works.

huangapple
  • 本文由 发表于 2017年9月13日 04:04:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/46184525.html
匿名

发表评论

匿名网友

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

确定