OpenSwoole\Coroutine\parallel

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

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 September 1, 2022