Closed yingjie52 closed 1 year ago
希望当不使用打印函数的时候,CPU正常
你得 pprof 一下,看看是什么地方在耗 CPU。
你得 pprof 一下,看看是什么地方在耗 CPU。
我使用你的例子,注释掉myFunc中的打印,CPU也是升高。详细代码发在DIscussion里面了。刚学习go, pprof不会用,我在看看
你得 pprof 一下,看看是什么地方在耗 CPU。
var sum int32
func myFunc(i interface{}) { n := i.(int32) atomic.AddInt32(&sum, n) fmt.Printf("run with %d\n", n) }
func demoFunc() { time.Sleep(1 * time.Millisecond) //fmt.Println("Hello World!") }
func main() { defer ants.Release()
runTimes := 10000000
// Use the common pool. var wg sync.WaitGroup syncCalculateSum := func() { demoFunc() wg.Done() } for i := 0; i < runTimes; i++ { wg.Add(1) _ = ants.Submit(syncCalculateSum) } wg.Wait() fmt.Printf("running goroutines: %d\n", ants.Running()) fmt.Printf("finish all tasks.\n")
// Use the pool with a function, // set 10 to the capacity of goroutine pool and 1 second for expired duration. p, := ants.NewPoolWithFunc(1000, func(i interface{}) { myFunc(i) wg.Done() }) defer p.Release() // Submit tasks one by one. for i := 0; i < runTimes; i++ { wg.Add(1) = p.Invoke(int32(i)) } wg.Wait() fmt.Printf("running goroutines: %d\n", p.Running()) fmt.Printf("finish all tasks, result is %d\n", sum)
@yingjie52 你这个是并发上来, 且都在抢同一锁导致的
正如 @liangjfblue 所说,这里 CPU 飙升有很大可能是因为锁竞争,而且我看你的 HTTP 请求是直接访问 127.0.0.1
,这样的话除非有特殊的路由配置否则这一类请求是不走网卡的,因此是非常快的,所以这里大概也没能给并发抢锁争取缓冲时间,所以导致了 CPU 飙高。
这个 issue 我先关闭了,如果不是这里所说的原因那么后面可以再打开,谢谢!
Actions I've taken before I'm here
What happened?
ants.NewPoolWithFunc(1000, dorequest): 当dorequest函数中不使用fmt.Println时,CPU耗尽; 当dorequest使用打印函数时,CPU在50%左右
Major version of ants
v2
Specific version of ants
v2.7.0
Operating system
Windows
Relevant log output
Code snippets (optional)
How to Reproduce
Steps to reproduce the behavior:
Does this issue reproduce with the latest release?
I haven't verified it with the latest release