dongyou26216708 2018-12-26 12:33
浏览 54
已采纳

所有goroutine都处于睡眠状态-死锁(无限循环+选择)

I have an app which creates, every few seconds, a routine fetching the current price from an API. It then sends the response to Monitor routine for analysis. If the Monitor finds that the price has changed significantly, it sends a notification.

It works fine if the delay between each go routine is large. It doesn't, if it is small: "fatal error: all goroutines are asleep - deadlock!" gets triggered and the program crashes.

I'm guessing (?) that the deadlock is caused by either (1) the Monitor being flooded with new price information (and not managing to analyse them timely); or (2) the Main function being flooded by messages from the Monitor.

There is probably also some weakness in the fact that the Main function appends the new price to a slice, while the Monitor is iterating through it.

How can this be resolved? Reading other posts here I thought that the "select" statement would be the magic cure, but it doesn't seem to be...

func main() {
    ec := make(chan entry, 10)
    mce := make (chan entry, 10)
    mcn := make(chan string, 10)

    go monitor(mce, mcn)

    for {
        go fetchData(ec)
    select {
        // get entries
        case newEntry := <- ec:
            log = append(log, newEntry)
            mce <- newEntry
        default:
            {}
        }

        //check if any notifications received
        select {
        case newMsg := <- mcn:
            fmt.Println(newMsg)
        default:
            {}
        }

        delay()
    }
}

func monitor(mce <-chan entry, mcn chan<- string) {


    for {
        newEntry = <- mce

        for _, item := range log {
            // here - do some analysis comparing the newEntry against previous entries
            // (essentially to see if notification should be triggered)
        }

        if should_send_notification {
            mcn <- msg
        }

    }
}

func fetchData(ec chan<- entry) {

    // here some code fetching newEntry from APIs

    // send the newEntry back to the main function
    ec <- newEntry

}
  • 写回答

1条回答 默认 最新

  • douxiyi2418 2018-12-26 13:37
    关注

    You only need merge the select on one and remove the default statement on main function. Removing the default statement you don't need a delay() function because the select-case works blocking and waiting some message from channels:

    func main() {
        ec := make(chan entry, 10)
        mce := make (chan entry, 10)
        mcn := make(chan string, 10)
    
        go monitor(mce, mcn)
        go fetchData(ec)
        for {
            select {
                // get entries
                case newEntry := <- ec:
                    log = append(log, newEntry)
                    mce <- newEntry
                //check if any notifications received
                case newMsg := <- mcn:
                    fmt.Println(newMsg)
            }
        }
    }
    

    The fetchData(ec) is convenient to implement like blocking and do not call it continuously:

    func fetchData(ec chan<- entry) {
    
        for {
            // here some code fetching newEntry from APIs
            // waiting data
    
            // send the newEntry if I get data
            ec <- newEntry
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值