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\parallel(int $n, callable $fn)
Set the number to concurrent coroutines
The function to be executed
if success, it returns TRUE, otherwise it returns FALSE.
Execute the function within multiple coroutines in concurrently.
since v4.5.3
<?php
declare(strict_types=1);
use Swoole\Coroutine;
use Swoole\Runtime;
use function Swoole\Coroutine\parallel;
Co\run(function() {
$c = 4;
$results = [];
parallel($c, function () use (&$results) {
System::usleep(200000);
$results[] = System::gethostbyname('localhost');
});
var_dump($results);
});