swoole http或wesocket单个进程内的on->open等,会不会一个没处理完先暂停,就开启下一个?

我的理解是,on->open内的处理完,有协程等待的也要完成后,才开启下另一个on->open协程 不知道对不对?

最佳答案

开启了协程,并且程序内有阻塞的协程代码co::开始的,就会暂停,记录下测试代码

            #echo "\n1|".'进程:'.$http->worker_id.'|fd:'.$request->fd.'|协程:'.Swoole\Coroutine::getCid().'开始时间:'.sprintf('%.8f',microtime(true));
            C('fd',$request->fd);
            $response->header("Content-Type", "text/html; charset=utf-8");
            $response->end('1');
            //sleep(1);
            if(C('fd')!=$request->fd){
                echo "\n2".C('fd').'|'.$request->fd;
            }
            #echo "\n3|".'进程:'.$http->worker_id.'|fd:'.$request->fd.'|协程:'.Swoole\Coroutine::getCid().'结束时间:'.sprintf('%.8f',microtime(true));
            echo "\n2|".'进程:'.$http->worker_id.'|fd:'.$request->fd.'|协程:'.Swoole\Coroutine::getCid().'|connections length:'.count($http->connections);

通过下在代码测试 如果协程内有等待会暂停了,开启另一个open

$user = posix_getpwnam('www');
posix_setuid($user['uid']);
posix_setgid($user['gid']);
$redis=new Redis;
$redis->pconnect('127.0.0.1', 6379); 
$redis->del('set'); 
$redis->set('redis_new',0); 
$pool = new RedisPool();

//创建websocket服务器对象,监听0.0.0.0:9502端口
$ws = new swoole_websocket_server("0.0.0.0", 9502);
$ws->set(array(
    'max_connection'=>20000,
    'worker_num' => 1,    //开启两个worker进程
    'max_request' => 0,   //每个worker进程max request设置为3次
    'dispatch_mode'=>2,
    'heartbeat_check_interval' => 1800,//5秒侦测一次心跳,一个TCP在10秒内未向服务器端发送数据,将会被切断
    'heartbeat_idle_time' => 1810,
    'user'=>'www',
    'group'=>'www'
));
//监听WebSocket连接打开事件
$ws->on('open', function ($ws, $request)use($pool) {
    $redis = $pool->get($ws->worker_id,$request->fd);
    $redis->sadd('set',$request->fd);
    echo "\n1|进程:".$ws->worker_id.'|fd:'.$request->fd.'|conns:'.count($ws->connections).'|redisconns:'.$redis->scard('set').'|协程:'.Swoole\Coroutine::getCid();    
    $pool->put($redis);
});

//监听WebSocket消息事件
$ws->on('message', function ($ws, $frame) {
    echo "\n2|进程:".$ws->worker_id.'|fd:'.$frame->fd.'|conns:'.count($ws->connections).'|协程:'.Swoole\Coroutine::getCid();    

});

//监听WebSocket连接关闭事件
$ws->on('close', function ($ws, $fd)use($pool) {
    $redis = $pool->get($ws->worker_id,$fd);
    $redis->srem('set',$fd);
    echo "\n3|进程:".$ws->worker_id.'|fd:'.$fd.'closed'.'|conns:'.count($ws->connections).'|redisconns:'.$redis->scard('set').'|协程:'.Swoole\Coroutine::getCid();
    $pool->put($redis);
});
$ws->start();












push($redis);
    }
    
    public static function count()
    {
       return self::p()->length();
    }
    

    public static function get()
    {
        //有空闲连接 或都创建了  
        if (self::p()->length() > 0 || self::len()==0 ){
            return self::p()->pop();
        }
        self::len(-1);
        //无空闲连接,创建新连接
        $redis = new Swoole\Coroutine\Redis();
        do{
            $res = $redis->connect(C('REDIS_HOST'), C('REDIS_PORT'));
        }while(!$res);

        if ('' != C('REDIS_PASSWORD')){
            $redis->auth(C('REDIS_PASSWORD'));
        }
        if (0 != C('REDIS_SELECT')){
            $redis->select(C('REDIS_SELECT'));
        }
        return $redis;
    }
}