Swoole\Coroutine\batch

Declaration

<?php
Swoole\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 Swoole\Coroutine;
use function Swoole\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);
});