情況:未開啟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