简体   繁体   English

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

[英]Websockets running in background of go program causing 100% CPU usage

I've implemented web sockets in my go program to regularly update three variables in the background while other processes are happening.我在我的 go 程序中实现了网络套接字,以便在其他进程发生时定期更新后台的三个变量。 Since doing this the program has begun taking up 100% of the CPU usage almost immediately and I'm unsure as to why.由于这样做,程序几乎立即开始占用 100% 的 CPU 使用率,我不确定为什么。

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.任何帮助找出导致 CPU 使用率大幅飙升的原因将不胜感激。

This happens when you have an infinite loop spinning in a goroutine.当您在 goroutine 中进行无限循环旋转时,就会发生这种情况。 It's hard to see where that's happening with the code subset you've posted.很难看出您发布的代码子集发生了什么。 But that's the reason why.但这就是原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM