swoole中协程被阻塞
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());
}
});
现象:执行php demo.php长时间阻塞
因为 $chan->isEmpty() === true
while 里面的循环不会被执行
阻塞的原因是因为 $chan 容量为默认为1
第二个push阻塞了