OpenSwoole\Coroutine\batch

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

Declaration

<?php OpenSwoole\Coroutine\batch(array $tasks, float $timeout = -1)

Parameters

tasks

An array of functions to be executed

timeout

The timeout of the batch tasks

Return

success

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

Description

This is a helper function to batch executing multiple tasks concurrently and wait all tasks to be finished.

Example

<?php
declare(strict_types=1);

use OpenSwoole\Coroutine;
use function OpenSwoole\Coroutine\batch;

co::run(function() {
    $tasks = [
        function () {
            Coroutine::sleep(1);
            return 'a';
        },
        'k1' =>  function () {
            Coroutine::sleep(1);
            return 'b';
        },
        'k2' =>  function () {
            Coroutine::sleep(1);
            return 'c';
        }
    ];

    $result = batch($tasks);
    var_dump($result);
});
Last updated on September 1, 2022