OpenSwoole\Core\Coroutine\WaitGroup->wait

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

Declaration

<?php OpenSwoole\Core\Coroutine\WaitGroup->wait(int $timeout = -1)

Parameters

timeout

The max time waiting for the group of coroutines to be finished.

Return

success

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

Description

Wait for all the coroutines to be finished. You can set a max timeout value. By default the main coroutine is waiting infinitely.

You have to install OpenSwoole core library with composer require openswoole/core to use this feature.

Example

<?php
declare(strict_types=1);

use OpenSwoole\Core\Coroutine\WaitGroup;

co::run(function() {
    $wg = new WaitGroup();

    go(function () use ($wg) {
        $wg->add();
        co::sleep(3);
        $wg->done();
    });

    go(function () use ($wg) {
        $wg->add();
        co::sleep(7);
        $wg->done();
    });

    $wg->wait(10);
});
Last updated on February 9, 2023