use Hyperf\Parallel\Parallel;
$parallel = new Parallel();
$parallel->add(function () {
sleep(3);
return 012;
});
$parallel->add(function () {
sleep(2);
return 123;
});
$parallel->add(function () {
sleep(1);
return 231;
});
$result = $parallel->wait();
return $result;
use Swoole\Coroutine\WaitGroup;
$wg = new WaitGroup();
for ($i = 0; $i < 3; $i++) {
$wg->add(1);
Swoole\Coroutine::create(function () use ($wg, $i) {
sleep($i + 1);
echo "Coroutine {$i} done.\n";
$wg->done();
});
}
$wg->wait();
echo "All coroutines done.\n";
use Swoole\Coroutine\Channel;
$chan = new Channel(3);
for ($i = 0; $i < 3; $i++) {
Swoole\Coroutine::create(function () use ($chan, $i) {
$data = "Coroutine {$i} data";
echo "Sending {$data}...\n";
$chan->push($data);
});
}
Swoole\Coroutine::create(function () use ($chan) {
for ($i = 0; $i < 3; $i++) {
echo "Receiving...\n";
$data = $chan->pop();
echo "Received {$data}.\n";
}
$chan->close();
});