get_coroutines
(PHP 8.6+, True Async 1.0)
get_coroutines() — 返回所有活跃协程的数组。适用于诊断和监控。
描述
get_coroutines(): array
返回值
Async\Coroutine 对象数组 — 当前请求中注册的所有协程。
示例
示例 #1 监控协程
<?php
use function Async\spawn;
use function Async\get_coroutines;
use function Async\delay;
spawn(function() { delay(10000); });
spawn(function() { delay(10000); });
// 让协程启动
delay(10);
foreach (get_coroutines() as $coro) {
echo sprintf(
"Coroutine #%d: %s, spawned at %s\n",
$coro->getId(),
$coro->isSuspended() ? 'suspended' : 'running',
$coro->getSpawnLocation()
);
}
?>
示例 #2 检测泄漏
<?php
use function Async\get_coroutines;
// 在请求结束时,检查未完成的协程
$active = get_coroutines();
if (count($active) > 0) {
foreach ($active as $coro) {
error_log("Unfinished coroutine: " . $coro->getSpawnLocation());
}
}
?>
参见
- current_coroutine() — 当前协程
- Coroutines — 协程概念