Swoole\Coroutine\WaitGroup->wait

Notice: the documentation is suitable for PHP extension swoole until v4.7.1, we only support PHP extension openswoole from v4.7.1

Declaration

<?php
Swoole\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.

Example

<?php
declare(strict_types=1);

use Swoole\Coroutine\WaitGroup;

Co\run(function() {
    $wg = new WaitGroup();

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

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

    $wg->wait(1);
});