Swoole Co-path blocking problem

< H2 > Synergetic path is blocked in swoole < / H2 >

demo.php:

Co::set([
    "trace_flags" => SWOOLE_TRACE_CLOSE
]);


$chan = new \Swoole\Coroutine\Channel();
function task1(\Swoole\Coroutine\Channel $chan) {
    Co::sleep(0.005);
    $chan->push([__METHOD__=>__LINE__]);
}
function task2(\Swoole\Coroutine\Channel $chan) {
    Co::sleep(0.005);
    $chan->push([__METHOD__=>__LINE__]);
}
go("task1", $chan);
go("task2", $chan);
go(function () use ($chan){
    while(!$chan->isEmpty()) {
        var_dump($chan->pop());
    }
});

phenomenon: the execution of php demo.php blocks for a long time

Apr.20,2022

because the loop in $chan- > isEmpty () = true
while will not be blocked
because the $chan capacity defaults to 1
the second push blocks


Why did you quit immediately when you only ran one trip? @ seeker

$chan = new \Swoole\Coroutine\Channel();
function task1(\Swoole\Coroutine\Channel $chan) {
    Co::sleep(0.005);
    $chan->push([__METHOD__=>__LINE__]);
}
go("task1", $chan);
go(function () use ($chan){
    // isEmpty===true,
    while(!$chan->isEmpty()) {
        var_dump($chan->pop());
    }
});
Menu