Swoole\Coroutine\parallel

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

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::usleep(200000);
        $results[] = System::gethostbyname('localhost');
    });
    var_dump($results);
});
Last updated on August 31, 2022