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长时间阻塞
1 回答
暮色呼如
TA贡献1853条经验 获得超9个赞
因为 $chan->isEmpty() === true
while 里面的循环不会被执行
阻塞的原因是因为 $chan 容量为默认为1
第二个push阻塞了
- 1 回答
- 0 关注
- 1236 浏览
添加回答
举报
0/150
提交
取消