我正在寻找一种可靠的方法来确保Go中的空信道不会阻止我的执行。我必须以特定顺序(优先级)迭代多个频道,一旦我找到一个包含项目的频道,请阅读一个。
目前我以类似的方式做事:
if len(myChannel) > 0 {
// Possible issue here: length could have changed to 0 making this blocking
elm := <- myChannel
return elm
}
从理论上讲,这可能会导致等待时间过长,而另一个频道可能会有一个准备好服务的项目&#34;。
有关如何改进的任何建议?我可以在频道中使用互斥锁,但感觉这是一个更好的解决方案,虽然我不确定如何。
答案 0 :(得分:2)
有reflect.Select
函数可以执行您想要的操作:
package main
import (
"fmt"
"reflect"
"time"
)
func main() {
a, b, c := make(chan int), make(chan int), make(chan int)
go func() {
time.Sleep(2 * time.Second)
a <- 1
}()
go func() {
time.Sleep(time.Second)
b <- 2
}()
go func() {
time.Sleep(3 * time.Second)
c <- 3
}()
for i := 0; i < 3; i++ {
chosen, recv, ok := reflect.Select([]reflect.SelectCase{
reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(a),
},
reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(b),
},
reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(c),
},
})
if ok {
fmt.Printf("Got value %d from %d\n", recv.Interface().(int), chosen)
}
}
}
答案 1 :(得分:-1)
I'm not sure this really answers the question "Is there a reliable way to ensure a Go channel does not block". In the OP's use case, it's OK if a recv blocks /so long as no other channel in the set would not block/, and that's what the accepted solution implements. This is different from "ensure the recv does not block", and I see no way around the fundamental limitation of the race condition OP points out.