Closed yuntian001 closed 3 years ago
ok
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
use function Swoole\Coroutine\run;
run(function(){
$channel = new Channel(1);
Coroutine::create(function () use ($channel) {
for($i = 0; $i < 10; $i++) {
Coroutine::sleep(1.0);
$channel->push(['rand' => rand(1000, 9999), 'index' => $i]);
echo "{$i}\n";
}
});
Coroutine::create(function () use ($channel) {
while(1) {
$data = $channel->pop(1);
if ($data) {
var_dump($data);
} else {
assert($channel->errCode === SWOOLE_CHANNEL_TIMEOUT);
break;
}
}
});
});
ok
use Swoole\Coroutine; use Swoole\Coroutine\Channel; use function Swoole\Coroutine\run; run(function(){ $channel = new Channel(1); Coroutine::create(function () use ($channel) { for($i = 0; $i < 10; $i++) { Coroutine::sleep(1.0); $channel->push(['rand' => rand(1000, 9999), 'index' => $i]); echo "{$i}\n"; } }); Coroutine::create(function () use ($channel) { while(1) { $data = $channel->pop(1); if ($data) { var_dump($data); } else { assert($channel->errCode === SWOOLE_CHANNEL_TIMEOUT); break; } } }); });
用上述代码依然会报错,错误信息 swoole 版本为4.6.2
===================================================================
[FATAL ERROR]: all coroutines (count: 1) are asleep - deadlock!
===================================================================
[Coroutine-2]
--------------------------------------------------------------------
#0 Swoole\Coroutine\Channel->push() called at
pop(2.0)
https://wiki.swoole.com/#/coroutine/channel?id=%e4%bd%bf%e7%94%a8%e7%a4%ba%e4%be%8b
实际内容: Coroutine\Channel使用示例
当运行结束会报死锁错误: