Swoole\Coroutine\parallel

Notice: the documentation is suitable for PHP extension swoole until v4.7.1, we only support PHP extension openswoole from v4.7.1

Declaration

<?php
Swoole\Coroutine\parallel(int $n, callable $fn)

Parameters

n

Set the number to concurrent coroutines

fn

The function to be executed

Return

success

if success, it returns TRUE, otherwise it returns FALSE.

Description

Execute the function within multiple coroutines in concurrently.

since v4.5.3

Example

<?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::sleep(0.2);
        $results[] = System::gethostbyname('localhost');
    });
    var_dump($results);
});