Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\Coroutine\batch(array $tasks, float $timeout = -1)
An array of functions to be executed
The timeout of the batch tasks
if success, it returns TRUE, otherwise it returns FALSE.
This is a helper function to batch executing multiple tasks concurrently and wait all tasks to be finished.
<?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);
});