Function Referenceget_coroutines()

get_coroutines ​

(PHP 8.6+, True Async 1.0)

get_coroutines() — Returns an array of all active coroutines. Useful for diagnostics and monitoring.

Description ​

php
get_coroutines(): array

Return Values ​

An array of Async\Coroutine objects — all coroutines registered in the current request.

Examples ​

Example #1 Monitoring coroutines ​

php
<?php
use function Async\spawn;
use function Async\get_coroutines;
use function Async\delay;

spawn(function() { delay(10000); });
spawn(function() { delay(10000); });

// Let the coroutines start
delay(10);

foreach (get_coroutines() as $coro) {
    echo sprintf(
        "Coroutine #%d: %s, spawned at %s\n",
        $coro->getId(),
        $coro->isSuspended() ? 'suspended' : 'running',
        $coro->getSpawnLocation()
    );
}
?>

Example #2 Detecting leaks ​

php
<?php
use function Async\get_coroutines;

// At the end of a request, check for unfinished coroutines
$active = get_coroutines();
if (count($active) > 0) {
    foreach ($active as $coro) {
        error_log("Unfinished coroutine: " . $coro->getSpawnLocation());
    }
}
?>

See Also ​