Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
<?php Swoole\Coroutine\Channel->pop(float $timeout = -1): mixed
The timeout period to wait when the channel is empty, the default will block and wait for data to be available
The return value can be any type of PHP variable, including anonymous functions and resources.
Reads and removes one item of data from the channel.
Wait and pop up one element from the queue of the channel.
<?php
$chan = new Swoole\Coroutine\Channel(1);
Co\run(function () use ($chan) {
$cid = Swoole\Coroutine::getuid();
$i = 0;
while (1) {
co::sleep(1);
$chan->push(['rand' => rand(1000, 9999), 'index' => $i]);
echo "[coroutine $cid] - $i\n";
$i++;
}
});
Co\run(function () use ($chan) {
$cid = Swoole\Coroutine::getuid();
while(1) {
$data = $chan->pop();
echo "[coroutine $cid]\n";
var_dump($data);
}
});
When the channel is closed, the execution fails and returns false
.
You can use $channel->errCode
to access the error code for more detail on what happened.