简体   繁体   中英

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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