Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
<?php Swoole\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 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);
});