确保Go频道的可靠方法不会阻止 [英] Reliable way to ensure a Go channel does not block

查看:193
本文介绍了确保Go频道的可靠方法不会阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种可靠的方法来确保Go中的空闲频道不会阻止我的执行。我必须按照特定的顺序(优先顺序)遍历多个渠道,并且一旦找到一个包含项目的渠道,请阅读一个渠道。



目前我做类似的方式:

  if len(myChannel)> 0 {
//可能的问题在这里:长度可能已经变为0使得这个阻塞
elm:=< - myChannel
return elm
}

从理论上讲,这可能导致等待时间过长,而不同的频道可能有一个项目已准备好服务 。

有关如何改进的建议?我可以在频道中使用互斥锁,但感觉好像有一个更好的解决方案,但我不知道如何。

有一个 reflect.Select 函数可以做你想做的:

 包主要

导入(
fmt
反映
时间


func main( ){
a,b,c:= make(chan int),make(chan int),make(chan int)
去func(){
time.Sleep(2 * time。第二)
a < - 1
}()
go func(){
time.Sleep(time.Second)
b < - 2
}()
去func(){
time.Sleep(3 * time.Second)
c< - 3
}()
for i:= 0 ;我< 3; i ++ {
selected,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(从%d\\\
得到值%d,recv.Interface()。(int),选择)
}
}
}

play .golang.org


I'm looking for a reliable to way to make sure an empty channel in Go does not block my execution. I have to iterate through a number of channels in a particular order (kind of priorities), and once I find one with items in it, read one.

Currently I do something in a similar way:

if len(myChannel) > 0 {
    // Possible issue here: length could have changed to 0 making this blocking
    elm := <- myChannel
    return elm
}

In theory this could result into too-long of waiting, while a different channel might have an item which is ready to be "served".

Any suggestions on how to improve? I could use a mutex in the channel, but it feels like there's a better solution although I'm not sure how.

解决方案

There is a reflect.Select function that might do what you want:

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)
        }
    }
}

play.golang.org

这篇关于确保Go频道的可靠方法不会阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆