swoole cpu占用很高 porcess

情况:未开启swoole cup占用20%,开启swoole cup占用飙升到70

各种排查...哪怕是放以下那么点代码,cpu立马飙升70%

 $SystemNoticeProcess = new SwooleProcess(function($process) use ($server) {
         

        });

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

解决办法:增加sleep休眠

 $SystemNoticeProcess = new SwooleProcess(function($process) use ($server) {

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

        });

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

扩展:

 $SystemNoticeProcess = new SwooleProcess(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

原文地址:https://www.cnblogs.com/wesky/p/12108594.html