package main
import (
"runtime"
)
func main() {
runtime.GOMAXPROCS(3)
go task()
go task()
select{}
}
func task(){
a := 100
for {
a += 1
}
}
上面代碼正常情況下會使cpu使用率達到60%左右,
但是如果我們想要是cpu使用率控制現在25%左右的話,得借助cpulimit命令:
yum install cpulimit //安裝cpulimit
cpulimit -l 25 -p 3306 //25:cpu的上限值,3306:進程pid號
之后用top查看cpu:
package main import ( "fmt" "runtime" ) func main() { n := runtime.NumCPU() fmt.Print(n) runtime.GOMAXPROCS(n - 1) //設置cpu運行的數目 }
Go從1.5版本開始,默認采用多核執行,默認是你的CPU核心數,以前版本默認為1
————————————————
版權聲明:本文為CSDN博主「YMY_mine」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/YMY_mine/article/details/103367272