dougengqiu8031
dougengqiu8031
采纳率100%
2018-03-05 07:08 阅读 263
已采纳

在go程序的后台运行的Websockets导致100%CPU使用率

I've implemented web sockets in my go program to regularly update three variables in the background while other processes are happening. Since doing this the program has begun taking up 100% of the CPU usage almost immediately and I'm unsure as to why.

Here is the code in question:

streamOneHandler := func(event *websockets.Event) {
    varOne, err = strconv.ParseFloat(event.Number, 64)
}

streamTwoHandler := func(event *websockets.Event) {
    varTwo, err = strconv.ParseFloat(event.Number, 64)
}

streamThreeHandler := func(event *websockets.Event) {
    varThree, err = strconv.ParseFloat(event.Number, 64)
}

errHandler := func(err error) {
    fmt.Println(err)
}


streamOne, err = websockets.WsEventServe("string1", streamOneHandler, errHandler )

    if err != nil {
        log.Fatal(err)
    }

streamTwo, err = websockets.WsEventServe("string2", streamTwoHandler, errHandler )

    if err != nil {
        log.Fatal(err)

streamThree, err = websockets.WsEventServe("string3", streamThreeHandler, errHandler )

    if err != nil {
        log.Fatal(err)
    }


go func() {
    <- streamOne
    <- streamTwo
    <- streamThree
}()

Any help figuring out what's causing this huge spike in CPU usage would be much appreciated.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    duanqiao1949 duanqiao1949 2018-03-05 18:55

    This happens when you have an infinite loop spinning in a goroutine. It's hard to see where that's happening with the code subset you've posted. But that's the reason why.

    点赞 评论 复制链接分享

相关推荐