Swoole\Coroutine\parallel

Notice: ext-swoole is supported until v4.7.1, use ext-openswoole >= v4.7.1. Latest version: pecl install openswoole-4.9.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);
});