swoole cpu占用很高 porcess


情況:未開啟swoole cup占用20%,開啟swoole cup占用飆升到70

 

各種排查...哪怕是放以下那么點代碼,cpu立馬飆升70%

 

 $SystemNoticeProcess = new Swoole\Process(function($process) use ($server) {
         

        });

        // 推送系統通知SystemNotice
        $this->server->addProcess($SystemNoticeProcess);

 

解決辦法:增加sleep休眠

 $SystemNoticeProcess = new Swoole\Process(function($process) use ($server) {

            sleep(1);  // cup降低最佳方案

        });

        // 推送系統通知SystemNotice
        $this->server->addProcess($SystemNoticeProcess);

 

擴展:

 $SystemNoticeProcess = new Swoole\Process(function($process) use ($server) {
            // 獲取隊列長度
            $i = $this->redis()->llen('SystemNotice');
            // 客戶端連接數>=1
            if(count($this->server->connections)>=1) {

                    // 獲取第一個元素
                    $redis_list = $this->redis()->Lpop('SystemNotice');
                    if ($redis_list) {
                        foreach ($this->server->connections as $conn) {
                            //$this->server->push($conn,json_encode(['type' => 'market','date' => $show_market_list]));
                            $this->server->push($conn, $redis_list);
                            // sleep(1)  //cup會有所降低,但依然很高
                        }
                    }
            }
            sleep(1);  // cup降低最佳方案

        });

        // 推送系統通知SystemNotice
        $this->server->addProcess($SystemNoticeProcess);

 

這算是swoole process的一個bug

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM