OpenSwoole\Coroutine\parallel

25.x is outdated, please check the latest version 26.x


Latest version: pecl install openswoole-26.2.0

Declaration

<?php OpenSwoole\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 OpenSwoole\Coroutine;
use OpenSwoole\Runtime;
use function OpenSwoole\Coroutine\parallel;

co::run(function() {
    $c = 4;
    $results = [];
    parallel($c, function () use (&$results) {
        System::usleep(200000);
        $results[] = System::gethostbyname('localhost');
    });
    var_dump($results);
});
Last updated on February 28, 2026